Skip to content

Commit 71ce747

Browse files
authored
Merge pull request #6 from MDaniel96/master
feat(logger): do not throw error when non error object is given
2 parents b983738 + fdf3efe commit 71ce747

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/logger/logger.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class Logger {
4848
}
4949

5050
_shortenStackTrace(stack) {
51+
if (typeof stack === 'undefined') {
52+
return;
53+
}
54+
5155
return stack.length > STACK_TRACE_LIMIT
5256
? stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
5357
: stack;
@@ -66,6 +70,10 @@ class Logger {
6670
}
6771

6872
_getErrorDetails(error) {
73+
if (!(error instanceof Object)) {
74+
return {};
75+
}
76+
6977
const baseDetails = {
7078
error_name: error.name,
7179
error_stack: this._shortenStackTrace(error.stack),

src/logger/logger.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ describe('Logger', function() {
213213
expect(logArguments.error_message).to.eql(error.message);
214214
expect(logArguments.error_data.length).to.eql(3004);
215215
});
216+
217+
describe('when not an Error instance is passed as error', function () {
218+
[
219+
{ type: 'custom object', value: {} },
220+
{ type: 'string', value: 'error' },
221+
{ type: 'null', value: null },
222+
{ type: 'number', value: 12 },
223+
{ type: 'bool', value: true }
224+
].forEach(({ type, value }) => {
225+
it(`should not throw error when ${type} is passed as error`, function () {
226+
expect(() => logger.customError('error', 'hi', value, { details: 'here' })).to.not.throw();
227+
});
228+
});
229+
230+
it('should log error properties from custom error object', function () {
231+
const errorObject = { name: 'Error', message: 'My custom error', stack: 'Stack', data: { value: 1 } };
232+
233+
logger.customError('error', 'hi', errorObject, { details: 'here' });
234+
235+
const logArguments = JSON.parse(Logger.config.output.args[0]);
236+
237+
expect(logArguments.error_name).to.eql(errorObject.name);
238+
expect(logArguments.error_stack).to.eql(errorObject.stack);
239+
expect(logArguments.error_message).to.eql(errorObject.message);
240+
expect(logArguments.error_data).to.eql(JSON.stringify(errorObject.data));
241+
});
242+
243+
it('should not log additional or missing error properties from custom error object', function () {
244+
const errorObject = { color: 'color', value: 'value' };
245+
246+
logger.customError('error', 'hi', errorObject, { details: 'here' });
247+
248+
const logArguments = JSON.parse(Logger.config.output.args[0]);
249+
250+
expect(logArguments).to.not.have.any.keys('error_name', 'error_stack', 'error_message', 'error_data');
251+
expect(logArguments).to.not.have.any.keys('color', 'value');
252+
});
253+
});
216254
});
217255

218256
describe('#configure', function() {

0 commit comments

Comments
 (0)