Created
April 6, 2021 12:50
-
-
Save chengjianhua/2f83cdd28e5f5ccc0d428b55eef6d285 to your computer and use it in GitHub Desktop.
[test Error.captureStackTrace()]
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
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