0

Queries:

  1. Why does the try block not catch the error in the asynchronous method in Javascript? Could it be understood in terms of execution context?

  2. Is there a way in which we could wrap all error handling in the entry file or at a higher level?

Context: In a multi-layer application (e.g. controller, service, DAO) in PHP, an umbrella try-catch block in the controller layer could catch the errors in any of the lower layers. Is it possible to do something similar in node.js?

Irvin Sandoval
  • 650
  • 8
  • 16
  • Does this answer your question? [How to catch a global error with NodeJS](https://stackoverflow.com/questions/34186146/how-to-catch-a-global-error-with-nodejs) – bricksphd Mar 20 '21 at 11:07

2 Answers2

0

A Promise is resolved only after all of the synchronous code has been finished running. As you say, it can be understood as execution context. In order for a Promise to resolve, it needs to perform extra steps, such as suspending the execution of the code. For this reason, unless you use await or a callback (.catch(() => {...})), you won't be able to catch the error the way you imagine.

You can await/callback all your async processes, and add a try catch block/catch callback that executes a function, at the top level of your application, though if you use REST, you might have to place the catches in individually.

0

@veryVerde is correct, that you need await or .catch().

Here is an article that explains what happens when you throw in different context within javascript (note the article focuses on the client side, not node.js): Article

Here is a some code that shows what does/doesn't get caught across various permutations of async and promises. (Or on jsfiddle: https://jsfiddle.net/bricksphd/dwpocs7k/21/.)

async function f() {
  //Throw without async or promise
  try {
    throw new ("Simple throw error: 1");
  } catch (e) {
    console.error("Caught in simple catch: " + e);
  }
  //
  //Throw async...
  //
  //...inside a catch block w/o await w/o catch
  try {
    f = async () => {
      throw ("Throw error in async w/o .catch: 2");
    }
    f();
  } catch (e) {
    console.error(e);
  }
  //...inside a catch block with w/o await with catch 
  try {
    f = async () => {
      throw "Async with .catch: 3";
    }
    f()
    .then(result => console.log("No error"))
    .catch(e => {console.log("Caught in promise.catch: " + e)});
  } catch (e) {
    console.error(e);
  }
  //...inside a catch block with await 
  try {
    f = async () => {
      throw ("Throw error with await/catch: 4");
    }
    await f();
  } catch (e) {
    console.error(e);
  }
  //
  //Promises...
  //
  //... inside a catch block with .catch
  try{
    let p = new Promise((resolve, reject)=>reject("Reject with .catch: 5"));
    p.then(resolve=>console.log("No error here.")).catch(e=>console.log(e));
  } catch (e){
    console.log("Caught Exception: " + e);
  }
  //... inside a catch block w/o .catch
  try{
    let p = new Promise((resolve, reject)=>reject("Reject w/o .catch: 6"));
  } catch (e){
    console.log("Caught Exception: " + e);
  }
  try{
    let p = new Promise((resolve, reject)=>reject("Reject with await: 7"));
    await p;
  } catch (e){
    console.log("Caught Exception: " + e);
  }
}
f()
.then(result=>console.log("Didn't find any global errors"))
.catch(e=>console.log("Found uncaught error: " + e));
bricksphd
  • 129
  • 9