Last active
May 26, 2021 00:15
-
-
Save DiegoVictor/aa8c2ae45e911dd4068bfbcc93da18b7 to your computer and use it in GitHub Desktop.
How to catch uncaught exceptions into Node.js
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
// 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