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]' > ```
This commit is contained in:
parent
ea07915e6b
commit
43b47fd349
2 changed files with 30 additions and 26 deletions
|
@ -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();
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
it('should return error stack trace if present', () => {
|
||||
const error = new Error('boom');
|
||||
assert.typeOf(error, 'Error');
|
||||
|
||||
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.');
|
||||
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, '<Original stack missing>');
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue