Skip to content

Commit

Permalink
Add standardErrors and fileSystemErrors copy to lang file
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Oct 17, 2023
1 parent 38dec98 commit 72011d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
9 changes: 9 additions & 0 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,15 @@ en:
marketing-email:
label: "Marketing emails"
errorHandlers:
standardErrors:
errorOccurred: "Error: {{ error }}"
errorContext: "Context: {{ context }}"
systemErrorOccurred: "A system error has occured: {{ errorMessage }}"
genericErrorOccured: "A {{ name }} has occurred."
unknownErrorOccured: "An unknown error has occured"
fileSystemErrors:
errorOccured: "An error occurred while {{ fileAction }} {{ filepath }}."
errorExplanation: "This is the result of a system error: {{ errorMessage }}"
apiErrors:
messageDetail: "{{ request }} in account {{ accountId }}"
unableToUpload: 'Unable to upload "{{ payload }}.'
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/lib/errorHandlers/fileSystemErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
isSystemError,
debugErrorAndContext,
} = require('./standardErrors');
const { i18n } = require('../lang');

const i18nKey = 'cli.lib.errorHandlers.fileSystemErrors';

class FileSystemErrorContext extends ErrorContext {
constructor(props = {}) {
Expand Down Expand Up @@ -35,10 +38,12 @@ function logFileSystemErrorInstance(error, context) {
const filepath = context.filepath
? `"${context.filepath}"`
: 'a file or folder';
const message = [`An error occurred while ${fileAction} ${filepath}.`];
const message = [i18n(`${i18nKey}.errorOccured`, { fileAction, filepath })];
// Many `fs` errors will be `SystemError`s
if (isSystemError(error)) {
message.push(`This is the result of a system error: ${error.message}`);
message.push(
i18n(`${i18nKey}.errorExplanation`, { errorMessage: error.message })
);
}
logger.error(message.join(' '));
debugErrorAndContext(error, context);
Expand Down
36 changes: 22 additions & 14 deletions packages/cli/lib/errorHandlers/standardErrors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { HubSpotAuthError } = require('@hubspot/cli-lib/lib/models/Errors');
const { logger } = require('@hubspot/cli-lib/logger');
const { i18n } = require('../lang');

const i18nKey = 'cli.lib.errorHandlers.standardErrors';

const isSystemError = err =>
err.errno != null && err.code != null && err.syscall != null;
Expand All @@ -22,18 +25,22 @@ class ErrorContext {
function debugErrorAndContext(error, context) {
if (error.name === 'StatusCodeError') {
const { statusCode, message, response } = error;
logger.debug('Error: %o', {
statusCode,
message,
url: response.request.href,
method: response.request.method,
response: response.body,
headers: response.headers,
});
logger.debug(
i18n(`${i18nKey}.errorOccured`, {
error: {
statusCode,
message,
url: response.request.href,
method: response.request.method,
response: response.body,
headers: response.headers,
},
})
);
} else {
logger.debug('Error: %o', error);
logger.debug(i18n(`${i18nKey}.errorOccured`, { error }));
}
logger.debug('Context: %o', context);
logger.debug(i18n(`${i18nKey}.errorContect`, { context }));
}

/**
Expand All @@ -44,7 +51,7 @@ function debugErrorAndContext(error, context) {
* @param {ErrorContext} context
*/
function logSystemError(error, context) {
logger.error(`A system error has occurred: ${error.message}`);
logger.error(i18n(`${i18nKey}.systemErrorOccured`, { error: error.message }));
debugErrorAndContext(error, context);
}

Expand All @@ -63,16 +70,17 @@ function logErrorInstance(error, context) {
if (error instanceof Error || error.message || error.reason) {
// Error or Error subclass
const name = error.name || 'Error';
const message = [`A ${name} has occurred.`];
[error.message, error.reason].forEach(msg => {
const message = i18n(`${i18nKey}.genericErrorOcurred`, { name })[
(error.message, error.reason)
].forEach(msg => {
if (msg) {
message.push(msg);
}
});
logger.error(message.join(' '));
} else {
// Unknown errors
logger.error(`An unknown error has occurred.`);
logger.error(i18n(`${i18nKey}.unknownErrorOcurred`));
}
debugErrorAndContext(error, context);
}
Expand Down

0 comments on commit 72011d0

Please sign in to comment.