0

I have a problem with JavaScript promises. Many times I intentionally "reject" them, and use the catch statement to process the rejected logic.

However the catch statement also triggers when there is a unhandled exception in my code, and I didn't mean to "reject" the promise.

I'm usually not console logging whatever comes into the catch function, and now my code just dies silently.

Is there any way to avoid this, and tell JavaScript to still console log these errors without console logging my own catch variables?

Edit to provide code example:

// Promise Function
function isEven(number){
  return new Promise((resolve, reject) => {
      if(number % 2 == 0){ resolve(); return; }
      reject();
  });
}

// Call Promise with logic handling
isEven(2).then(function(){

}).catch(function(err){

})

This works great to know if the number is even, but if I send an object by mistake instead of a number the code just silently dies. I know in this case I can test the catch response (err) for null as that's the "rejected" value, but do I have to custom filter this type of logic every time?

YAHsaves
  • 1,697
  • 12
  • 33
  • You need to put the `try/catch` only around the `await` for the promise. – Barmar Apr 04 '22 at 17:17
  • @Barmar I should specify that I'm not using the await syntax, but also can you provide a code example? I'm having trouble visualizing how that would help. – YAHsaves Apr 04 '22 at 17:18
  • 2
    When you `.catch()` an intentional rejection, check whether the error is actually the one you expect. If not, re-`throw` it. – Bergi Apr 04 '22 at 17:18
  • How are you getting an exception if you don't use `await`? That's what translates promise rejection into an exception that's caught with `try/catch`. You need to post a [mre] that demonstrates the problem you're having. – Barmar Apr 04 '22 at 17:19
  • @Bergi, is that really the only way to do it? Every single catch statement needs a custom filter to determine unhandled vs handled logic? That's a lot of boilerplate.... Is there a single "if" statment I can use to check for true "javascript" error messages? – YAHsaves Apr 04 '22 at 17:20
  • @YAHsaves No, there's no single check for that, but surely you know how to check for the errors you've thrown yourself (or the errors a library threw that you want to handle)? – Bergi Apr 04 '22 at 17:23
  • 2
    Catches (whether .catch or with async/await) catch errors. Whether you threw those errors on purpose or not is irrelevant to the functionality. So either check whether it's the error you expected, or look for an alternative way to represent the expected failures. – jonrsharpe Apr 04 '22 at 17:23
  • 1
    @Barmar OP is talking about exceptions in promise code, like `new Promise(() => oops)` or `promise.then(() => oops)`, which both lead to a `ReferenceError: undeclared variable 'oops'`. No need for `async`/`await` here. – Bergi Apr 04 '22 at 17:24
  • Other languages solve this by allowing you to specify the exception type to catch in the `catch` statement, e.g. PHP `catch (MyException $e) { ... }`. This allows that `try/catch` to ignore other exception types. Unfortunately, JS doesn't have this, so you need to write an explicit test and re-throw if it's not the type you want. – Barmar Apr 04 '22 at 17:26
  • 1
    Maybe one way you can handle this is by creating your own custom error (which would extend the `Error` class). This way you can achieve a "single if" condition to handle your own custom exceptions. So this way you could throw your custom error and handle it accordingly. Example: https://flaviocopes.com/javascript-custom-errors/. You can then either throw the error, or reject the promise using your custom error - https://javascript.info/promise-error-handling. – Daniel Grima Apr 04 '22 at 17:36
  • Have a look at https://stackoverflow.com/questions/21616432/why-are-exceptions-used-for-rejecting-promises-in-js/ and https://stackoverflow.com/questions/19528705/exception-handling-thrown-errors-within-promises – Bergi May 01 '22 at 13:39

0 Answers0