diff --git a/lib/errors.ts b/lib/errors.ts index d89a66b4..a8bdfaee 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -20,6 +20,7 @@ import * as os from 'os'; import { TypedError } from 'typed-error'; import { getChalk, stripIndent } from './utils/lazy'; import { getHelp } from './utils/messages'; +import { CliSettings } from './utils/bootstrap'; export class ExpectedError extends TypedError { public code?: string; @@ -104,6 +105,17 @@ function interpret(error: Error): string { return error.message; } +function loadDataDirectory(): string { + try { + const settings = new CliSettings(); + return settings.get('dataDirectory') as string; + } catch { + return os.platform() === 'win32' + ? 'C:\\Users\\\\_balena' + : '$HOME/.balena'; + } +} + const messages: { [key: string]: (error: Error & { path?: string }) => string; } = { @@ -127,6 +139,23 @@ const messages: { EACCES: (e) => messages.EPERM(e), + BalenaSettingsPermissionError: () => { + const dataDirectory = loadDataDirectory(); + + return stripIndent` + Error reading data directory: "${dataDirectory}" + + This error usually indicates that the user doesn't have permissions over that directory, + which can happen if balena CLI was executed as the root user. + + ${ + os.platform() === 'win32' + ? `Try resetting the ownership by opening a new Command Prompt as administrator and running: \`takeown /f ${dataDirectory} /r\`` + : `Try resetting the ownership by running: \`sudo chown -R $(whoami) ${dataDirectory}\`` + } + `; + }, + ETIMEDOUT: () => 'Oops something went wrong, please check your connection and try again.', @@ -151,6 +180,7 @@ const messages: { // related issue https://github.com/balena-io/balena-sdk/issues/1025 // related issue https://github.com/balena-io/balena-cli/issues/2126 const EXPECTED_ERROR_REGEXES = [ + /^BalenaSettingsPermissionError/, // balena-settings-storage /^BalenaAmbiguousApplication/, // balena-sdk /^BalenaAmbiguousDevice/, // balena-sdk /^BalenaApplicationNotFound/, // balena-sdk diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 61b073a0..f3abbee6 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -2534,18 +2534,6 @@ "balena-settings-storage": "^7.0.0", "jwt-decode": "^2.2.0", "tslib": "^2.0.0" - }, - "dependencies": { - "balena-settings-storage": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/balena-settings-storage/-/balena-settings-storage-7.0.0.tgz", - "integrity": "sha512-gufzVJznyt9e1CvpBuLe2caU5KcEwl1YHCbK5OMz09zXDA2OMAICPXsLlViK+KiuZwZrBx3tyU2FZjAzRZFgwQ==", - "requires": { - "@types/node": "^10.17.26", - "balena-errors": "^4.7.1", - "tslib": "^2.0.0" - } - } } }, "balena-config-json": { @@ -2793,11 +2781,12 @@ } }, "balena-settings-storage": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/balena-settings-storage/-/balena-settings-storage-6.0.1.tgz", - "integrity": "sha512-jdDoKzbJXlF696EZSbwD6lZ1dMe98aUtx7btFE4j4PRCSeh2BWx5P5VLGh9Bk3sH2FUcqYg0iw/wdKvkcv44oA==", + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/balena-settings-storage/-/balena-settings-storage-7.0.0.tgz", + "integrity": "sha512-gufzVJznyt9e1CvpBuLe2caU5KcEwl1YHCbK5OMz09zXDA2OMAICPXsLlViK+KiuZwZrBx3tyU2FZjAzRZFgwQ==", "requires": { "@types/node": "^10.17.26", + "balena-errors": "^4.7.1", "tslib": "^2.0.0" } }, diff --git a/package.json b/package.json index e69e6f7f..5e1f9130 100644 --- a/package.json +++ b/package.json @@ -204,7 +204,7 @@ "balena-sdk": "^15.20.0", "balena-semver": "^2.3.0", "balena-settings-client": "^4.0.5", - "balena-settings-storage": "^6.0.1", + "balena-settings-storage": "^7.0.0", "balena-sync": "^11.0.2", "bluebird": "^3.7.2", "body-parser": "^1.19.0",