Skip to content

Commit

Permalink
check before trying to extend objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorlang committed Nov 19, 2015
1 parent c7065ea commit 70ca194
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions stack-chain.js
Expand Up @@ -117,9 +117,11 @@ function prepareStackTrace(error, originalFrames) {
frames = frames.slice(0, Error.stackTraceLimit);

// Set the callSite property
// But only if it havn't been explicitly set, otherwise
// error.stack would have unintended side effects
if (Object.getOwnPropertyDescriptor(error, "callSite") === undefined) {
// But only if it hasn't been explicitly set, otherwise
// error.stack would have unintended side effects. Check also for
// non-extensible/sealed objects, such as those from Google's Closure Library
if (Object.isExtensible(error) &&
(Object.getOwnPropertyDescriptor(error, "callSite") === undefined)) {
error.callSite = {
original: originalFrames,
mutated: frames
Expand Down
29 changes: 29 additions & 0 deletions test/simple/non-extensible-errors.js
@@ -0,0 +1,29 @@
var test = require("tap").test;
var chain = require('../../');

test("non extensible Error objects don't throw", function(t) {
var error = new Error("don't extend me");
Object.preventExtensions(error)
t.doesNotThrow(function() {
error.stack;
});
t.end();
});

test('stack is correct on non extensible error object', function (t) {
var error = new Error("don't extend me");
Object.preventExtensions(error);

chain.format.replace(function () {
return 'good';
});

try {
t.equal(error.stack, 'good');
} catch (e) { t.ifError(e); }

chain.format.restore();

t.end();
});

0 comments on commit 70ca194

Please sign in to comment.