Skip to content

Instantly share code, notes, and snippets.

@rmorlang
Created November 18, 2015 17:46
Show Gist options
  • Save rmorlang/a7f89993e639a7c569be to your computer and use it in GitHub Desktop.
Save rmorlang/a7f89993e639a7c569be to your computer and use it in GitHub Desktop.
Example code that demonstrates how stack-chain will blow up if an error has been made non-extensible for some reason.
function createNonExtensibleError() {
var error = new Error("I am a non-extensible error");
Object.preventExtensions(error);
return error;
}
function handleError(e) {
console.log("\nERROR: " + e.message);
console.log("--- STACK TRACE BEGIN ---");
console.log(e.stack);
console.log("--- STACK TRACE END ---");
}
// Demonstrate that raising a non-exentensible error is safe.
try {
throw createNonExtensibleError();
} catch (error) {
handleError(error);
}
// The above results in output like this:
//
// ERROR: I am a non-extensible error
// --- STACK TRACE BEGIN ---
// Error: I am a non-extensible error
// at createNonExtensibleError (/home/rlm/ws/stack-chain-test/test.js:2:15)
// at Object.<anonymous> (/home/rlm/ws/stack-chain-test/test.js:16:9)
// at Module._compile (module.js:435:26)
// at Object.Module._extensions..js (module.js:442:10)
// at Module.load (module.js:356:32)
// at Function.Module._load (module.js:311:12)
// at Function.Module.runMain (module.js:467:10)
// at startup (node.js:136:18)
// at node.js:963:3
// --- STACK TRACE END ---
// Now require stack-chain....
var chain = require('stack-chain');
// Demonstrate that non-extensible errors blow us up
try {
throw createNonExtensibleError();
} catch (error) {
handleError(error);
}
// The above results in output like this:
//
// ERROR: I am a non-extensible error
// --- STACK TRACE BEGIN ---
// /home/rlm/ws/stack-chain-test/test.js:10
// console.log(e.stack);
// ^
// TypeError: Cannot define property:callSite, object is not extensible.
// at Function.defineProperty (native)
// at Error.Object.defineProperty.set (/home/rlm/ws/stack-chain-test/node_modules/stack-chain/stack-chain.js:177:12)
// at Function.prepareStackTrace (/home/rlm/ws/stack-chain-test/node_modules/stack-chain/stack-chain.js:123:20)
// at handleError (/home/rlm/ws/stack-chain-test/test.js:10:16)
// at Object.<anonymous> (/home/rlm/ws/stack-chain-test/test.js:28:3)
// at Module._compile (module.js:435:26)
// at Object.Module._extensions..js (module.js:442:10)
// at Module.load (module.js:356:32)
// at Function.Module._load (module.js:311:12)
// at Function.Module.runMain (module.js:467:10)
console.log("This code never executes, because stack-chain throws an error.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment