Skip to content

Instantly share code, notes, and snippets.

@DiegoVictor
Last active May 26, 2021 00:15
Show Gist options
  • Save DiegoVictor/aa8c2ae45e911dd4068bfbcc93da18b7 to your computer and use it in GitHub Desktop.
Save DiegoVictor/aa8c2ae45e911dd4068bfbcc93da18b7 to your computer and use it in GitHub Desktop.
How to catch uncaught exceptions into Node.js
// https://nodejs.org/api/process.html
process.on("uncaughtException", (err) => {
console.log("Of course we can!");
console.log(`We catch a ${err.name}`);
});
class CallError extends Error {
constructor(message) {
super(message);
console.log(message);
this.name = "CallError";
this.message = message;
}
}
async function call() {
return new Promise((_, reject) => {
reject(new CallError("Can we catch this error?"));
}).catch(() => {
console.log("Yes, we can!");
return null;
});
}
call().then((profile) => {
if (!profile) {
throw new CallError("And this?");
}
});
// Output:
// Can we catch this error?
// Yes, we can!
// And this?
// Of course we can!
// We catch a CallError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment