2017-12-20 21:46:01 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2017 Resin.io
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-05-03 16:59:12 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as Raven from 'raven';
|
|
|
|
import * as Promise from 'bluebird';
|
2018-05-03 12:48:01 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
|
|
|
|
2018-05-03 16:59:12 +00:00
|
|
|
import * as patterns from './utils/patterns';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
const captureException = Promise.promisify<string, Error>(
|
|
|
|
Raven.captureException,
|
|
|
|
{ context: Raven },
|
|
|
|
);
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-05-03 12:48:01 +00:00
|
|
|
function hasCode(error: any): error is Error & { code: string } {
|
|
|
|
return error.code != null;
|
|
|
|
}
|
|
|
|
|
2018-05-03 13:15:22 +00:00
|
|
|
function treatFailedBindingAsMissingModule(error: any): void {
|
|
|
|
if (error.message.startsWith('Could not locate the bindings file.')) {
|
|
|
|
error.code = 'MODULE_NOT_FOUND';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:48:01 +00:00
|
|
|
function interpret(error: any): string | undefined {
|
|
|
|
if (!(error instanceof Error)) {
|
|
|
|
return;
|
2018-05-03 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
treatFailedBindingAsMissingModule(error);
|
|
|
|
|
|
|
|
if (hasCode(error)) {
|
2018-05-03 12:48:01 +00:00
|
|
|
const errorCodeHandler = messages[error.code];
|
|
|
|
const message = errorCodeHandler && errorCodeHandler(error);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEmpty(error.message)) {
|
|
|
|
return `${error.code}: ${error.message}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return error.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages: {
|
2018-05-03 16:59:12 +00:00
|
|
|
[key: string]: (error: Error & { path?: string }) => string;
|
2018-05-03 12:48:01 +00:00
|
|
|
} = {
|
2018-05-03 16:59:12 +00:00
|
|
|
EISDIR: error => `File is a directory: ${error.path}`,
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2018-05-03 16:59:12 +00:00
|
|
|
ENOENT: error => `No such file or directory: ${error.path}`,
|
2018-05-03 12:48:01 +00:00
|
|
|
|
|
|
|
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.
|
2018-05-03 16:59:12 +00:00
|
|
|
${
|
|
|
|
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`.'
|
2018-05-03 12:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
If this is not the case, and you're trying to burn an SDCard, check that the write lock is not set.`,
|
|
|
|
|
2018-05-03 16:59:12 +00:00
|
|
|
EACCES: e => messages.EPERM(e),
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2018-05-03 16:59:12 +00:00
|
|
|
ETIMEDOUT: () =>
|
|
|
|
'Oops something went wrong, please check your connection and try again.',
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2018-05-03 13:15:22 +00:00
|
|
|
MODULE_NOT_FOUND: () => stripIndent`
|
|
|
|
Part of the CLI could not be loaded. This typically means your CLI install is in a broken state.
|
2018-05-03 16:59:12 +00:00
|
|
|
${
|
|
|
|
os.arch() === 'x64'
|
|
|
|
? 'You can normally fix this by uninstalling and reinstalling the CLI.'
|
|
|
|
: stripIndent`
|
2018-05-03 13:15:22 +00:00
|
|
|
You're using an unsupported architecture (${os.arch()}), so this is typically caused by missing native modules.
|
|
|
|
Reinstalling may help, but pay attention to errors in native module build steps en route.
|
|
|
|
`
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
|
2018-05-03 12:48:01 +00:00
|
|
|
ResinExpiredToken: () => stripIndent`
|
|
|
|
Looks like your session token is expired.
|
|
|
|
Please try logging in again with:
|
2018-05-03 16:59:12 +00:00
|
|
|
$ resin login`,
|
|
|
|
};
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
exports.handle = function(error: any) {
|
2018-05-03 12:48:01 +00:00
|
|
|
let message = interpret(error);
|
2018-01-09 15:05:24 +00:00
|
|
|
if (message == null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
message = error.stack;
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:48:01 +00:00
|
|
|
patterns.printErrorMessage(message!);
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
return captureException(error)
|
2018-01-09 15:05:24 +00:00
|
|
|
.timeout(1000)
|
|
|
|
.catch(function() {
|
|
|
|
// Ignore any errors (from error logging, or timeouts)
|
|
|
|
})
|
|
|
|
.finally(() => process.exit(error.exitCode || 1));
|
2017-12-20 21:46:01 +00:00
|
|
|
};
|