question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

throw in Async route

See original GitHub issue

If i throw an error on async function crash the server.

  app.post('/auth/google', async (req: any, res: any) => {
    throw new Error('Invalid google login');
    return;
  });

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
jkyberneeescommented, Apr 18, 2020

Hi @sarriaroman, I would need to know more details on your service configuration. I can tell you nor 0http or restana consider environment variables on their implementation.

You can confirm with this example:

'use strict'
process.env.ENV = 'PRODUCTION'

const service = require('../index')({
  errorHandler (err, req, res) {
    console.log(`Unexpected error: ${err.message}`)
    res.send(err)
  }
})

service.use(async (req, res, next) => {
  try {
    await next()
  } catch (err) {
    return next(err)
  }
})

service.get('/throw', (req, res) => {
  throw new Error('Upps!')
})
service.use('/nested', router)

service.start()

It might be that you are registering connect like middlewares that are not returning next() value, and therefore not propagating the response promise. I recommend you to register the following middleware right before your routes:

service.use(async (req, res, next) => {
  try {
    await next()
  } catch (err) {
    return next(err)
  }
})

Regards

2reactions
deman4ikcommented, Jul 10, 2020

Hello. I am also had the same issue. Would recommend to put this snippet into docs.

service.use(async (req, res, next) => {
  try {
    await next()
  } catch (err) {
    return next(err)
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling errors in express async middleware - Stack Overflow
The easy way, is to add try/catch inside your middleware, and call next(err) . app. get('/route', async(req, res, next) => { try {...
Read more >
Async Error Handling in Node.js + Express | by Nate Jones
"Errors that occur in synchronous code inside route handlers and middleware require no extra work. If synchronous code throws an error, ...
Read more >
throw in Async route · Issue #81 · BackendStack21/restana
If i throw an error on async function crash the server. app.post('/auth/google', async (req: any, res: any) => { throw new Error('Invalid google...
Read more >
Error Handling in Express JS and express-async-errors package
Error handling middleware must have four parameters error, request, response and next. It must be placed at the end of middleware functions.
Read more >
Error handling - Express.js
Errors that occur in synchronous code inside route handlers and middleware require no extra work. If synchronous code throws an error, then Express...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found