Skip to content

Commit

Permalink
Fixes mdlavin#12: Work around Bluebird long stack traces chomping 'Ca…
Browse files Browse the repository at this point in the history
…used by'
  • Loading branch information
Druotic committed Aug 26, 2017
1 parent 7eb0c20 commit fb0bd87
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
var inherits = require('inherits');

function buildNestedCause (nested) {
var append = '\n';

// Bluebird long stack traces will drop any
// trace lines that don't match /^\s*at\s*/
// see https://github.com/mdlavin/nested-error-stacks/issues/12
if (process.env.BLUEBIRD_DEBUG || process.env.BLUEBIRD_LONG_STACK_TRACES) {
append += 'at ';
}
append += 'Caused By: ' + nested.stack;
return append;
}

var NestedError = function (message, nested) {
this.nested = nested;

Expand All @@ -21,7 +34,7 @@ var NestedError = function (message, nested) {
get: function () {
var stack = oldStackDescriptor.get.call(this);
if (this.nested) {
stack += '\nCaused By: ' + this.nested.stack;
stack += buildNestedCause(this.nested);
}
return stack;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"inherits": "~2.0.1"
},
"devDependencies": {
"bluebird": "^3.5.0",
"chai": "~1.9.1",
"mocha": "~1.18.2",
"uuid": "~1.4.1"
Expand Down
61 changes: 61 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,65 @@ describe('NestedErrors', function () {
expect(messageDescriptor).to.be.undefined;
});

context('Bluebird long stack trace support', function () {
var origEnv;

beforeEach(function () {
delete require.cache[require.resolve('bluebird')]
origEnv = process.env;
});

afterEach(function () {
process.env = origEnv;
});

it('without Bluebird long stack traces, includes child stack in \
stacktrace without \'at\' prepended', function () {
var Promise = require('bluebird');

var childMessage = uuid.v1();
var child = new Error(childMessage);
var message = uuid.v1();
var nested = new NestedError(message, child);

return Promise.reject(child)
.catch(function (err) {
expect(nested.stack).to.contain('Caused By: ' + child.stack);
expect(nested.stack).to.not.contain('at Caused By: ' + child.stack);
});
});

it('with BLUEBIRD_DEBUG=1, includes child stack in stacktrace \
with \'at\' prepended', function () {
process.env.BLUEBIRD_DEBUG=1;
var Promise = require('bluebird');

var childMessage = uuid.v1();
var child = new Error(childMessage);
var message = uuid.v1();
var nested = new NestedError(message, child);

return Promise.reject(child)
.catch(function (err) {
expect(nested.stack).to.contain('at Caused By: ' + child.stack);
});
});

it('with BLUEBIRD_LONG_STACK_TRACES=1, includes child stack in \
stacktrace with \'at\' prepended', function () {
process.env.BLUEBIRD_LONG_STACK_TRACES=1;
var Promise = require('bluebird');

var childMessage = uuid.v1();
var child = new Error(childMessage);
var message = uuid.v1();
var nested = new NestedError(message, child);

return Promise.reject(child)
.catch(function (err) {
expect(nested.stack).to.contain('at Caused By: ' + child.stack);
});
});
});

});

0 comments on commit fb0bd87

Please sign in to comment.