Created
August 26, 2019 19:52
-
-
Save krisselden/0d618af20c64e81f18f1a058178292bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create error with caused by and formatted stack trace while preserving | |
* the laziness of the stack trace serialization. | |
*/ | |
function createErrorWithCausedBy(name, message, causedBy) { | |
const error = new Error(message); | |
error.name = name; | |
if (Error.captureStackTrace) { | |
// hides createErrorWithCausedBy from the stack | |
Error.captureStackTrace(error, createErrorWithCausedBy); | |
} | |
let stack; | |
// we have to create another prototype chain because | |
// even though in V8 the stack is lazy on access it is | |
// a value type property | |
const wrapper = Object.create(error); | |
Object.defineProperty(wrapper, "stack", { | |
enumerable: false, | |
configurable: true, | |
get() { | |
if (stack === undefined) { | |
stack = `${error.stack}\nCaused by: ${causedBy.stack}`; | |
} | |
return stack; | |
}, | |
set(val) { | |
stack = val; | |
} | |
}); | |
wrapper.causedBy = causedBy; | |
return wrapper; | |
} | |
function causeError() { | |
return a; | |
} | |
function doSomething() { | |
try { | |
causeError(); | |
} catch (e) { | |
throw createErrorWithCausedBy( | |
"DoSomethingError", | |
"Failed to do something.", | |
e | |
); | |
} | |
} | |
try { | |
doSomething(); | |
} catch (error) { | |
if (typeof process !== "undefined") { | |
// node | |
console.error( | |
`Format with O: | |
%O | |
Format with o: | |
%o | |
e.toString(): | |
%s | |
e.stack: | |
%s`, | |
error, | |
error, | |
error.toString(), | |
error.stack | |
); | |
} else { | |
// assume d8 --allow-natives-syntax | |
print("\nerror"); | |
debugPrint(error); | |
print("\nObject.getPrototypeOf(error)"); | |
debugPrint(Object.getPrototypeOf(error)); | |
print("\nerror.causedBy"); | |
debugPrint(error.causedBy); | |
print(error.stack); | |
print("\n--- After accessing error.stack ---"); | |
print("\nObject.getPrototypeOf(error)"); | |
debugPrint(Object.getPrototypeOf(error)); | |
print("\nerror.causedBy"); | |
debugPrint(error.causedBy); | |
} | |
} | |
function debugPrint(obj) { | |
eval("%DebugPrint(obj)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment