2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2020-03-04 16:31:54 +00:00
|
|
|
Copyright 2016-2020 Balena
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
2018-05-03 16:59:12 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
import * as os from 'os';
|
2020-03-05 01:02:46 +00:00
|
|
|
import { TypedError } from 'typed-error';
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2020-03-05 01:02:46 +00:00
|
|
|
export class ExpectedError extends TypedError {}
|
2019-10-31 01:40:57 +00:00
|
|
|
|
|
|
|
export class NotLoggedInError extends ExpectedError {}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
export class InsufficientPrivilegesError extends ExpectedError {}
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 01:40:57 +00:00
|
|
|
function interpret(error: Error): string {
|
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}`;
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 01:40:57 +00:00
|
|
|
|
|
|
|
return error.message;
|
2018-05-03 12:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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`
|
2019-07-17 19:12:51 +00:00
|
|
|
You don't have sufficient 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-10-19 14:38:50 +00:00
|
|
|
BalenaExpiredToken: () => stripIndent`
|
2018-05-03 12:48:01 +00:00
|
|
|
Looks like your session token is expired.
|
|
|
|
Please try logging in again with:
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena login`,
|
2018-05-03 16:59:12 +00:00
|
|
|
};
|
2018-05-03 12:48:01 +00:00
|
|
|
|
2019-08-28 18:51:56 +00:00
|
|
|
export async function handleError(error: any) {
|
2019-10-31 01:40:57 +00:00
|
|
|
const { printErrorMessage } = await import('./utils/patterns');
|
|
|
|
|
|
|
|
process.exitCode =
|
|
|
|
error.exitCode === 0
|
|
|
|
? 0
|
|
|
|
: parseInt(error.exitCode, 10) || process.exitCode || 1;
|
|
|
|
|
|
|
|
if (!(error instanceof Error)) {
|
|
|
|
printErrorMessage(String(error));
|
2018-01-09 15:05:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-10-31 01:40:57 +00:00
|
|
|
const message = [interpret(error)];
|
|
|
|
|
|
|
|
if (process.env.DEBUG && error.stack) {
|
|
|
|
message.push(error.stack);
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2019-10-31 01:40:57 +00:00
|
|
|
printErrorMessage(message.join('\n'));
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-03-05 01:02:46 +00:00
|
|
|
const expectedErrorREs = [
|
|
|
|
/^BalenaApplicationNotFound:/, // balena-sdk
|
|
|
|
/^BalenaDeviceNotFound:/, // balena-sdk
|
|
|
|
/^Missing \w+$/, // Capitano's command line parsing error
|
|
|
|
/^Unexpected arguments?:/, // oclif's command line parsing error
|
|
|
|
];
|
|
|
|
|
|
|
|
if (
|
|
|
|
error instanceof ExpectedError ||
|
|
|
|
expectedErrorREs.some(re => re.test(message[0]))
|
|
|
|
) {
|
2019-10-31 01:40:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-10-31 01:40:57 +00:00
|
|
|
// Report "unexpected" errors via Sentry.io
|
2020-03-04 16:31:54 +00:00
|
|
|
const Sentry = await import('@sentry/node');
|
|
|
|
Sentry.captureException(error);
|
|
|
|
await Sentry.close(1000);
|
|
|
|
// Unhandled/unexpected error: ensure that the process terminates.
|
|
|
|
// The exit error code was set above through `process.exitCode`.
|
|
|
|
process.exit();
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|