Avoid `ensure-error` for privacy reasons

Example:

```
> node
> const nonError = {foo: 'i-am-private'};
undefined

// before
> util.inspect(nonError);
'{ foo: \'i-am-private\' }'

// after
> nonError.toString()
'[object Object]'
>
```
pull/1/head
Daniel Gasienica 7 years ago
parent ea07915e6b
commit 43b47fd349

@ -1,7 +1,12 @@
const ensureError = require('ensure-error');
// toLogFormat :: Error -> String // toLogFormat :: Error -> String
exports.toLogFormat = (error) => { exports.toLogFormat = (error) => {
const normalizedError = ensureError(error); if (!error) {
return normalizedError.stack; return error;
}
if (error && error.stack) {
return error.stack;
}
return error.toString();
}; };

@ -9,36 +9,35 @@ const APP_ROOT_PATH = Path.join(__dirname, '..', '..', '..');
describe('Errors', () => { describe('Errors', () => {
describe('toLogFormat', () => { describe('toLogFormat', () => {
it('should convert non-errors to errors', () => { it('should return error stack trace if present', () => {
try { const error = new Error('boom');
// eslint-disable-next-line no-throw-literal assert.typeOf(error, 'Error');
throw 'boom';
} catch (nonError) { const formattedError = Errors.toLogFormat(error);
assert.typeOf(nonError, 'string'); assert.include(formattedError, 'errors_test.js');
assert.isUndefined(nonError.stack); assert.include(formattedError, APP_ROOT_PATH, 'Formatted stack has app path');
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 add stack to errors without one', () => { it('should return error string representation if stack is missing', () => {
const error = new Error('boom'); const error = new Error('boom');
error.stack = null; error.stack = null;
assert.typeOf(error, 'Error'); assert.typeOf(error, 'Error');
assert.isNull(error.stack); assert.isNull(error.stack);
const formattedStack = Errors.toLogFormat(error); const formattedError = Errors.toLogFormat(error);
assert.include(formattedStack, '<Original stack missing>'); assert.strictEqual(formattedError, 'Error: boom');
assert.include(formattedStack, APP_ROOT_PATH, 'Formatted stack has app path'); });
[
0,
false,
null,
undefined,
].forEach((value) => {
it(`should return \`${value}\` argument`, () => {
const formattedNonError = Errors.toLogFormat(value);
assert.strictEqual(formattedNonError, value);
});
}); });
}); });
}); });

Loading…
Cancel
Save