From e7a8deed0568fc6600c1e49a634aebbd358dbbfa Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Thu, 3 May 2018 14:48:01 +0200 Subject: [PATCH] Inline the entire resin-cli-errors module It's awkward that error handling requires you to go to a different package, it makes things more complicated, and there's nowhere else that really should be reusing this logic. Let's inline it, so we can deprecate the module entirely. Change-Type: patch --- lib/errors.ts | 66 ++++++++++++++++++++++++++++++++--- package.json | 1 - typings/resin-cli-errors.d.ts | 1 - 3 files changed, 62 insertions(+), 6 deletions(-) delete mode 100644 typings/resin-cli-errors.d.ts diff --git a/lib/errors.ts b/lib/errors.ts index 6e9bf9ec..24ac5adc 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -14,18 +14,76 @@ See the License for the specific language governing permissions and limitations under the License. */ -import errors = require('resin-cli-errors'); -import patterns = require('./utils/patterns'); +import _ = require('lodash'); +import os = require('os'); import Raven = require('raven'); import Promise = require('bluebird'); +import { stripIndent } from 'common-tags'; + +import patterns = require('./utils/patterns'); const captureException = Promise.promisify( Raven.captureException, { context: Raven }, ); +function hasCode(error: any): error is Error & { code: string } { + return error.code != null; +} + +function interpret(error: any): string | undefined { + if (!(error instanceof Error)) { + return; + } else if (hasCode(error)) { + const errorCodeHandler = messages[error.code]; + const message = errorCodeHandler && errorCodeHandler(error); + + if (message) { + return message; + } + + if (!_.isEmpty(error.message)) { + return `${error.code}: ${error.message}`; + } + + return; + } else { + return error.message; + } +} + +const messages: { + [key: string]: (error: Error & { path?: string }) => string +} = { + EISDIR: (error) => `File is a directory: ${error.path}`, + + ENOENT: (error) => `No such file or directory: ${error.path}`, + + ENOGIT: () => stripIndent` + Git is not installed on this system. + Head over to http://git-scm.com to install it and run this command again.`, + + EPERM: () => stripIndent` + You don't have enough privileges to run this operation. + ${os.platform() === 'win32' ? + 'Run a new Command Prompt as administrator and try running this command again.' : + 'Try running this command again prefixing it with `sudo`.' + } + + If this is not the case, and you're trying to burn an SDCard, check that the write lock is not set.`, + + EACCES: (e) => messages.EPERM(e), + + ETIMEDOUT: () => 'Oops something went wrong, please check your connection and try again.', + + ResinExpiredToken: () => stripIndent` + Looks like your session token is expired. + Please try logging in again with: + $ resin login` +} + exports.handle = function(error: any) { - let message = errors.interpret(error); + let message = interpret(error); if (message == null) { return; } @@ -34,7 +92,7 @@ exports.handle = function(error: any) { message = error.stack; } - patterns.printErrorMessage(message); + patterns.printErrorMessage(message!); return captureException(error) .timeout(1000) diff --git a/package.json b/package.json index bc1f8a96..d8bad806 100644 --- a/package.json +++ b/package.json @@ -133,7 +133,6 @@ "reconfix": "^0.0.3", "request": "^2.81.0", "resin-bundle-resolve": "^0.5.3", - "resin-cli-errors": "^1.2.0", "resin-cli-form": "^1.4.1", "resin-cli-visuals": "^1.4.0", "resin-compose-parse": "^1.8.1", diff --git a/typings/resin-cli-errors.d.ts b/typings/resin-cli-errors.d.ts deleted file mode 100644 index b62af69f..00000000 --- a/typings/resin-cli-errors.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'resin-cli-errors';