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
|
// 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) {
|
|
||||||
assert.typeOf(nonError, 'string');
|
|
||||||
assert.isUndefined(nonError.stack);
|
|
||||||
|
|
||||||
const formattedStack = Errors.toLogFormat(nonError);
|
const formattedError = Errors.toLogFormat(error);
|
||||||
assert.include(
|
assert.include(formattedError, 'errors_test.js');
|
||||||
formattedStack,
|
assert.include(formattedError, APP_ROOT_PATH, 'Formatted stack has app path');
|
||||||
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…
Add table
Add a link
Reference in a new issue