Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standard err serializer: better support for caused errors #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 13 additions & 15 deletions lib/bunyan.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,8 @@ function Logger(options, _childOptions, _childSimple) {
if (!options) {
throw new TypeError('options (object) is required');
}
if (!parent) {
if (!options.name) {
throw new TypeError('options.name (string) is required');
}
} else {
if (options.name) {
throw new TypeError(
'invalid options.name: child cannot set logger name');
}
if (!parent && !options.name) {
throw new TypeError('options.name (string) is required');
}
if (options.stream && options.streams) {
throw new TypeError('cannot mix "streams" and "stream" options');
Expand Down Expand Up @@ -985,7 +978,7 @@ Logger.stdSerializers.res = function res(res) {

/*
* This function dumps long stack traces for exceptions having a cause()
* method. The error classes from
* method or a cause property. The error classes from
* [verror](https://github.com/davepacheco/node-verror) and
* [restify v2.0](https://github.com/mcavage/node-restify) are examples.
*
Expand All @@ -995,11 +988,16 @@ Logger.stdSerializers.res = function res(res) {
function getFullErrorStack(ex)
{
var ret = ex.stack || ex.toString();
if (ex.cause && typeof (ex.cause) === 'function') {
var cex = ex.cause();
if (cex) {
ret += '\nCaused by: ' + getFullErrorStack(cex);
}
var cex;
switch (typeof ex.cause) {
case 'function':
cex = ex.cause();
break;
case 'object':
cex = ex.cause;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps else if here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, it could even be a switch statement.

}
if (cex) {
ret += '\ncaused by ' + getFullErrorStack(cex);
}
return (ret);
}
Expand Down