How to handle your own generated errors together with library / unhandled exceptions in Javascript promises? The problem I'm facing is generated by the "catch-all" nature of the catch()
method of promises.
For instance, I have the following code:
somePromiseFunction()
.then(result => {
// External library or behaviour
someCodeThatCanThrowExceptions()
if (result.code === 1) {
throw 'My own exception'
}
})
.catch(err => {
// how do I know if this is an exception I don't know or something
// that I do want to actually handle?
});
Lots of things can go wrong in the then()
chain, external libraries can throw exceptions and so on. For instance, I want to print "Generic error" when something like this happens.
But I also want to validate the code and exit early (like I'm doing in the example) and in this case actually print my own message "My own error".
How to handle both my "exit early" throw
s and all the different things than can go wrong during the execution of the code?