Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const { LEVEL, MESSAGE } = require('triple-beam');
* If the `message` property of the `info` object is an instance of `Error`,
* replace the `Error` object its own `message` property.
*
* Optionally, the Error's `stack` property can also be appended to the `info` object.
* Optionally, the Error's `stack` property can also be appended to the `message` property.
* For backward compatibility also include stack property into info object.
*/
module.exports = format((einfo, { stack }) => {
if (einfo instanceof Error) {
Expand All @@ -20,7 +21,12 @@ module.exports = format((einfo, { stack }) => {
[MESSAGE]: einfo[MESSAGE] || einfo.message
});

if (stack) info.stack = einfo.stack;
if (stack) {
info[MESSAGE] += '\n' + einfo.stack;
info.message = info[MESSAGE];
info.stack = einfo.stack;
}

return info;
}

Expand All @@ -34,6 +40,11 @@ module.exports = format((einfo, { stack }) => {
einfo[MESSAGE] = err.message;

// Assign the stack if requested.
if (stack) einfo.stack = err.stack;
if (stack) {
einfo[MESSAGE] += '\n' + err.stack;
einfo.message = einfo[MESSAGE];
einfo.stack = err.stack;
}

return einfo;
});
10 changes: 5 additions & 5 deletions test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ describe('errors()({ object })', () => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals('info');
assume(info.message).equals(err.message);
assume(info[MESSAGE]).equals(err.message);
assume(info.message).equals(err.message + '\n' + err.stack);
assume(info[MESSAGE]).equals(err.message + '\n' + err.stack);
assume(info.stack).equals(err.stack);
}
));
Expand Down Expand Up @@ -100,15 +100,15 @@ describe('errors()(Error)', () => {
{ immutable: false }
));

it('errors({ space: 2 }) sets info.stack', assumeFormatted(
it('errors({ stack: true }) includes error stack into message.', assumeFormatted(
errors({ stack: true }),
errInfo,
(info) => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.level).equals(errInfo.level);
assume(info.message).equals(errInfo.message);
assume(info[MESSAGE]).equals(errInfo.message);
assume(info.message).equals(errInfo.message + '\n' + errInfo.stack);
assume(info[MESSAGE]).equals(errInfo.message + '\n' + errInfo.stack);
assume(info.stack).equals(errInfo.stack);
},
{ immutable: false }
Expand Down