Skip to content

Commit

Permalink
refactor(CLI): Support decoratedMessage on ServerlessError
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 19, 2021
1 parent 73c071b commit 2217158
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/cli/handle-error.js
Expand Up @@ -156,9 +156,11 @@ module.exports = async (exception, options = {}) => {

// TODO: Ideally after migrating to new logger complete this strip should not be needed
// (it can be removed after we clear all chalk error message decorations from internals)
const errorMsg = stripAnsi(
exceptionTokens.stack && !isUserError ? exceptionTokens.stack : exceptionTokens.message
);
const errorMsg =
exceptionTokens.decoratedMessage ||
stripAnsi(
exceptionTokens.stack && !isUserError ? exceptionTokens.stack : exceptionTokens.message
);
writeText(null, style.error('Error:'), errorMsg);

legacy.consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
Expand Down
3 changes: 2 additions & 1 deletion lib/serverless-error.js
@@ -1,9 +1,10 @@
'use strict';

class ServerlessError extends Error {
constructor(message, code) {
constructor(message, code, options = {}) {
super(message);
this.code = code;
this.decoratedMessage = options.decoratedMessage;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/utils/tokenize-exception.js
Expand Up @@ -14,6 +14,7 @@ module.exports = (exception) => {
message: exception.message,
isUserError: userErrorNames.has(exception.name),
code: exception.code,
decoratedMessage: exception.decoratedMessage,
};
}
return {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/lib/utils/tokenize-exception.test.js
Expand Up @@ -7,13 +7,16 @@ const tokenizeError = require('../../../../lib/utils/tokenize-exception');

describe('test/unit/lib/utils/tokenize-exception.test.js', () => {
it('Should tokenize user error', () => {
const errorTokens = tokenizeError(new ServerlessError('Some error', 'ERR_CODE'));
const errorTokens = tokenizeError(
new ServerlessError('Some error', 'ERR_CODE', { decoratedMessage: 'decorated' })
);
expect(errorTokens.title).to.equal('Serverless Error');
expect(errorTokens.name).to.equal('ServerlessError');
expect(errorTokens.stack).to.include('tokenize-exception.test.js:');
expect(errorTokens.message).to.equal('Some error');
expect(errorTokens.isUserError).to.equal(true);
expect(errorTokens.code).to.equal('ERR_CODE');
expect(errorTokens.decoratedMessage).to.equal('decorated');
});

it('Should tokenize programmer error', () => {
Expand Down

0 comments on commit 2217158

Please sign in to comment.