Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Created April 6, 2021 12:50
Show Gist options
  • Save chengjianhua/2f83cdd28e5f5ccc0d428b55eef6d285 to your computer and use it in GitHub Desktop.
Save chengjianhua/2f83cdd28e5f5ccc0d428b55eef6d285 to your computer and use it in GitHub Desktop.
[test Error.captureStackTrace()]
class CustomError extends Error {
constructor(foo = "bar", ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
// if (Error.captureStackTrace) {
// Error.captureStackTrace(this, CustomError);
// }
this.name = "CustomError";
// Custom debugging information
this.foo = foo;
this.date = new Date();
}
}
function testStack1() {
// throw "error 1";
throw new CustomError("error 1");
// throw new Error("error 1");
}
function testStack2() {
throw new Error("error 2");
}
function testCaptureStack() {
try {
testStack1();
} catch (error) {
console.error(error);
console.log(error.message);
Error.captureStackTrace(error);
console.error(error);
}
}
testCaptureStack();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment