0
0
ExpressHow-ToBeginner · 3 min read

How to Use express-async-errors for Async Error Handling in Express

To use express-async-errors, simply install it and require it once in your Express app before defining routes. It automatically catches errors thrown in async route handlers and passes them to Express error middleware without extra try-catch blocks.
📐

Syntax

First, install the package with npm install express-async-errors. Then, require it at the top of your main server file with require('express-async-errors'). This patches Express to handle errors thrown in async functions automatically.

Define your async route handlers normally without try-catch. Errors thrown inside will be forwarded to your error-handling middleware.

javascript
const express = require('express')
require('express-async-errors')

const app = express()

app.get('/route', async (req, res) => {
  // async code that might throw
  throw new Error('Oops!')
})

app.use((err, req, res, next) => {
  res.status(500).send({ error: err.message })
})

app.listen(3000)
💻

Example

This example shows an Express app using express-async-errors to catch errors thrown in an async route without try-catch. The error is handled by the error middleware and a 500 response is sent.

javascript
const express = require('express')
require('express-async-errors')

const app = express()

app.get('/error', async (req, res) => {
  // Simulate an async error
  await Promise.reject(new Error('Async failure'))
})

app.use((err, req, res, next) => {
  res.status(500).json({ message: err.message })
})

app.listen(3000, () => console.log('Server running on port 3000'))
Output
Server running on port 3000 Request: GET /error Response: 500 { "message": "Async failure" }
⚠️

Common Pitfalls

Common mistakes include not requiring express-async-errors before routes, which means async errors won't be caught automatically. Also, forgetting to add error-handling middleware will cause unhandled errors.

Do not wrap async routes in try-catch if you use this package; it handles errors for you.

javascript
/* Wrong way: Missing require, errors not caught */
const express = require('express')
const app = express()

app.get('/fail', async (req, res) => {
  throw new Error('Not caught')
})

app.use((err, req, res, next) => {
  res.status(500).send('Error')
})

app.listen(3000)

/* Right way: Require express-async-errors first */
require('express-async-errors')
const express2 = require('express')
const app2 = express2()

app2.get('/fail', async (req, res) => {
  throw new Error('Caught correctly')
})

app2.use((err, req, res, next) => {
  res.status(500).send('Error handled')
})

app2.listen(3001)
📊

Quick Reference

  • Install with npm install express-async-errors
  • Require it once at the top of your main file: require('express-async-errors')
  • Write async route handlers without try-catch
  • Add Express error middleware to handle errors
  • Do not mix with manual try-catch for async errors

Key Takeaways

Require express-async-errors once before defining routes to enable async error handling.
Write async route handlers normally; errors thrown will be caught automatically.
Always add Express error-handling middleware to respond to errors.
Avoid manual try-catch in async routes when using express-async-errors.
Not requiring the package or missing error middleware causes unhandled errors.