javascript - Stop Execution of Code After Error thrown in Async Await function - Stack Overflow

I am creating a Nodejs and express based backend application and trying to handle error in a manner whi

I am creating a Nodejs and express based backend application and trying to handle error in a manner which is suitable for production systems.

I use async await to handle all synchronous operations in the code.

Here is a code snippet of router end points

app.get("/demo",async (req, res, next) => {
 await helper().catch(e => return next(e))
 console.log("After helper is called")
 res.json(1)
})

function helper(){ //helper function that throws an exception
 return new Promise((resolve, reject)=> reject(new Error("Demo Error")))
}

After all routes are defined I have added a mon error handler that catches exceptions. To simplify it I am adding a simple function

routes.use( (err, req, res, next) => {
  console.log("missed all", err)

 return res.status(500).json({error:err.name, message: err.message});
});

I expect that the code after await helper() should not execute since the exception has been handled and response sent to frontend. Instead what I get is this error.

After helper is called
(node:46) UnhandledPromiseRejectionWarning: Error 
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the 
client

What is the correct way to handle error with async await?

I am creating a Nodejs and express based backend application and trying to handle error in a manner which is suitable for production systems.

I use async await to handle all synchronous operations in the code.

Here is a code snippet of router end points

app.get("/demo",async (req, res, next) => {
 await helper().catch(e => return next(e))
 console.log("After helper is called")
 res.json(1)
})

function helper(){ //helper function that throws an exception
 return new Promise((resolve, reject)=> reject(new Error("Demo Error")))
}

After all routes are defined I have added a mon error handler that catches exceptions. To simplify it I am adding a simple function

routes.use( (err, req, res, next) => {
  console.log("missed all", err)

 return res.status(500).json({error:err.name, message: err.message});
});

I expect that the code after await helper() should not execute since the exception has been handled and response sent to frontend. Instead what I get is this error.

After helper is called
(node:46) UnhandledPromiseRejectionWarning: Error 
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the 
client

What is the correct way to handle error with async await?

Share Improve this question edited Mar 27, 2019 at 13:55 Kartik Jain asked Mar 27, 2019 at 13:50 Kartik JainKartik Jain 1092 silver badges10 bronze badges 1
  • You can have a look at this answer stackoverflow./questions/48685872/… – Shubham raj Commented Mar 27, 2019 at 13:58
Add a ment  | 

2 Answers 2

Reset to default 1

You get After helper is called, because your code continues to execute since it did not return

Don't chain catch with async/await. You do that with Promise.

helper()
  .then(data => console.log(data))
  .catch(e => console.log(e))

You can handle error like:

app.get("/demo",async (req, res, next) => {
  try {
    await helper();
    // respond sent if all went well
    res.json(something)
  catch(e) {
    // don't need to respond as you're doing that with catch all error handler
    next(e)
  }
})

you can use try catch to handle the situation

app.get("/demo",async (req, res, next) => {
 try {
  await helper()
  console.log("After helper is called")
  res.json(1)
 } catch(err) {
  next(err)
 }
})

function helper(){ //helper function that throws an exception
 return new Promise((resolve, reject)=> reject(new Error("Demo Error")))
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745579546a4634173.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信