0

I want to write a global handler overall express ASYNC body function.

I am using

app.get('/api/0/get/whisper', (req, res) => { 
    res.send(a.b.c)
})

In this case, I am getting an error ReferenceError: a is not defined and error handler working perfectly.

// error handler
app.use((err, req, res, next) => { 
   res.status(500).send(`Internal Server Error`)
})

How can I use this handler for ASYNC body like this:

app.get('/api/0/get/whisper', async (req, res) => { 
    res.send(a.b.c)
})

My error handler not working for async function. Why?

Thank you for help.

lisibuvowo
  • 15
  • 3
  • Duplicate of [Handling errors in express async middleware](https://stackoverflow.com/questions/51391080/handling-errors-in-express-async-middleware) – Marcos Casagrande Apr 02 '19 at 17:40
  • Check that other question, express does not handle promise rejections, you need to wrap the middleware or `try/catch` inside it. – Marcos Casagrande Apr 02 '19 at 17:40

0 Answers0