Skip to content

Instantly share code, notes, and snippets.

@SegersIan
Created April 17, 2019 13:04
Show Gist options
  • Save SegersIan/87dfa51c4a2eff64fd0c7b9a3d2bda87 to your computer and use it in GitHub Desktop.
Save SegersIan/87dfa51c4a2eff64fd0c7b9a3d2bda87 to your computer and use it in GitHub Desktop.
function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
function myFunctionThatCatches() {
try {
return thisThrows();
} catch (e) {
// Maybe do something else here first.
throw e;
} finally {
console.log('We do cleanup here');
}
}
async function run() {
try {
await myFunctionThatCatches();
} catch (e) {
console.error(e);
}
}
run();
// Outputs:
// We do cleanup here
// Error: Thrown from thisThrows()
// at thisThrows (/repo/error_stacktrace_2.js:2:11) <-- Notice we now point to the origin of the actual error
// at myFunctionThatCatches (/repo/error_stacktrace_2.js:7:16)
// at run (/repo/error_stacktrace_2.js:18:15)
// at Object.<anonymous> (/repo/error_stacktrace_2.js:24:1)
@Predarion
Copy link

This misses an await in line 7, should be:
return await thisThrows();

@SegersIan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment