diff --git a/js/modules/types/errors.js b/js/modules/types/errors.js index eba76af47..0914eacc2 100644 --- a/js/modules/types/errors.js +++ b/js/modules/types/errors.js @@ -1,7 +1,12 @@ -const ensureError = require('ensure-error'); - // toLogFormat :: Error -> String exports.toLogFormat = (error) => { - const normalizedError = ensureError(error); - return normalizedError.stack; + if (!error) { + return error; + } + + if (error && error.stack) { + return error.stack; + } + + return error.toString(); }; diff --git a/test/modules/types/errors_test.js b/test/modules/types/errors_test.js index 945ce68ee..9cca14062 100644 --- a/test/modules/types/errors_test.js +++ b/test/modules/types/errors_test.js @@ -9,36 +9,35 @@ const APP_ROOT_PATH = Path.join(__dirname, '..', '..', '..'); describe('Errors', () => { describe('toLogFormat', () => { - it('should convert non-errors to errors', () => { - try { - // eslint-disable-next-line no-throw-literal - throw 'boom'; - } catch (nonError) { - assert.typeOf(nonError, 'string'); - assert.isUndefined(nonError.stack); - - const formattedStack = Errors.toLogFormat(nonError); - assert.include( - formattedStack, - APP_ROOT_PATH, - 'Formatted stack has app path' - ); - return; - } - - // eslint-disable-next-line no-unreachable - assert.fail('Expected error to be thrown.'); + it('should return error stack trace if present', () => { + const error = new Error('boom'); + assert.typeOf(error, 'Error'); + + const formattedError = Errors.toLogFormat(error); + assert.include(formattedError, 'errors_test.js'); + assert.include(formattedError, APP_ROOT_PATH, 'Formatted stack has app path'); }); - it('should add stack to errors without one', () => { + it('should return error string representation if stack is missing', () => { const error = new Error('boom'); error.stack = null; assert.typeOf(error, 'Error'); assert.isNull(error.stack); - const formattedStack = Errors.toLogFormat(error); - assert.include(formattedStack, ''); - assert.include(formattedStack, APP_ROOT_PATH, 'Formatted stack has app path'); + const formattedError = Errors.toLogFormat(error); + assert.strictEqual(formattedError, 'Error: boom'); + }); + + [ + 0, + false, + null, + undefined, + ].forEach((value) => { + it(`should return \`${value}\` argument`, () => { + const formattedNonError = Errors.toLogFormat(value); + assert.strictEqual(formattedNonError, value); + }); }); }); });