Skip to content

Commit

Permalink
Always include error name and message in stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Jan 28, 2024
1 parent 7b5934f commit 45073bc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
25 changes: 18 additions & 7 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ function isCauseProperty(key) {
return key !== 'name' && key !== 'message' && key !== 'stack';
}

/**
* @param {string | Error | LogError} error
* @returns {string}
*/
function stack(error) {
if (typeof error === 'string') {
return error;
}
const msg = error.name && error.message
? `${error.name}: ${error.message}`
: error.name || String(error);
const s = error.stack;
return !s ? msg : s.indexOf(msg) === 0 ? s : `${msg}\n${s}`;
}

/**
* @param {LogEntry} entry
* @param {LogError} error
Expand All @@ -73,9 +88,9 @@ function addError(entry, error) {
}, {});
}
}
entry.stack = error.stack || String(error);
entry.stack = stack(error);
if (cause) {
entry.cause = cause.stack || String(cause);
entry.cause = stack(cause);
}
}

Expand Down Expand Up @@ -117,11 +132,7 @@ function write(ns, base_data, topic, msg, data, error) {
} else {
entry.data = base_data ? Object.assign({}, base_data, data) : data;
if (error) {
if (typeof error === 'string') {
entry.stack = error;
} else {
addError(entry, /** @type {LogError} */ (error));
}
addError(entry, /** @type {LogError} */ (error));
}
}
} else if (base_data) {
Expand Down
52 changes: 48 additions & 4 deletions test/log-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ describe('logger', () => {
+ '"data":{"the":"things"}}\n');
});

it('logs error', () => {
it('logs error without message', () => {
const error = new Error();

log.error('Oups', error);

assert.equals(out, '{"ts":123,"ns":"test","topic":"error","msg":"Oups",'
+ `"stack":${JSON.stringify(error.stack)}}\n`);
});

it('logs error with message', () => {
const error = new Error('Ouch!');

log.error('Oups', error);
Expand Down Expand Up @@ -224,11 +233,46 @@ describe('logger', () => {
+ '"data":{"name":"a","message":"b"}}\n');
});

it('logs message with error-like object { name, message, stack }', () => {
log.error({ name: 'SomeError', message: 'b', stack: 'Some issue' });
it('logs error-like object { name, message, stack } with error in stack', () => {
const error = {
name: 'SyntaxError',
message: 'Ouch!',
stack: 'SyntaxError: Ouch!\n at xyz:123'
};

log.error(error);

assert.equals(out, '{"ts":123,"ns":"test","topic":"error",'
+ `"stack":"SyntaxError: Ouch!\\n at xyz:123"}\n`);
});

it('logs error-like object { name, message }', () => {
const error = { name: 'SyntaxError', message: 'Ouch!' };

log.error('Test', {}, error);

assert.equals(out, '{"ts":123,"ns":"test","topic":"error","msg":"Test",'
+ `"data":{},"stack":"SyntaxError: Ouch!"}\n`);
});

it('logs error-like object { name, message, stack } without error in stack', () => {
const error = { name: 'SyntaxError', message: 'Ouch!', stack: ' at xyz:123' };

log.error(error);

assert.equals(out, '{"ts":123,"ns":"test","topic":"error",'
+ `"stack":"SyntaxError: Ouch!\\n at xyz:123"}\n`);
});

it('logs error-like cause { name, message, stack }', () => {
const error = new Error('Ouch!');
error.cause = { name: 'SyntaxError', message: 'Cause', stack: ' at xyz:123' };

log.error(error);

assert.equals(out, '{"ts":123,"ns":"test","topic":"error",'
+ '"stack":"Some issue"}\n');
+ `"stack":${JSON.stringify(error.stack)},`
+ `"cause":"SyntaxError: Cause\\n at xyz:123"}\n`);
});

it('logs data and error object', () => {
Expand Down

0 comments on commit 45073bc

Please sign in to comment.