0
0
ExpressHow-ToBeginner · 3 min read

How to Use next for Error Passing in Express

In Express, you use next(error) to pass an error to the next error-handling middleware. Calling next with an error skips normal middleware and triggers error handlers defined with four arguments.
📐

Syntax

The next function is used inside middleware or route handlers to pass control to the next middleware. When you pass an error object to next(error), Express skips all remaining non-error middleware and calls the error-handling middleware.

  • next(): Passes control to the next middleware.
  • next(error): Passes an error to error-handling middleware.
  • Error-handling middleware has four parameters: (err, req, res, next).
javascript
app.use((req, res, next) => {
  if (someErrorCondition) {
    const error = new Error('Something went wrong');
    next(error); // Pass error to error handler
  } else {
    next(); // Continue normally
  }
});

app.use((err, req, res, next) => {
  // Handle the error here
  res.status(500).send(err.message);
});
💻

Example

This example shows a route that triggers an error and passes it to the error handler using next(error). The error handler sends a 500 status with the error message.

javascript
import express from 'express';
const app = express();

app.get('/error', (req, res, next) => {
  const error = new Error('This is a forced error');
  next(error); // Pass error to error handler
});

// Error-handling middleware
app.use((err, req, res, next) => {
  res.status(500).json({ message: err.message });
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
Output
When you visit http://localhost:3000/error, the server responds with status 500 and JSON: {"message":"This is a forced error"}
⚠️

Common Pitfalls

Common mistakes when using next for error passing include:

  • Not defining error-handling middleware with four parameters (err, req, res, next), so Express won't recognize it as an error handler.
  • Calling next() without an error inside error-handling middleware, which can cause infinite loops.
  • Forgetting to call next(error) when an error occurs, causing the request to hang.
javascript
/* Wrong: Error handler missing 4 parameters */
app.use((err, req, res) => {
  res.status(500).send('Error');
});

/* Right: Error handler with 4 parameters */
app.use((err, req, res, next) => {
  res.status(500).send('Error');
});
📊

Quick Reference

  • Use next(error) to pass errors to error handlers.
  • Error handlers must have four arguments: (err, req, res, next).
  • Place error handlers after all other middleware and routes.
  • Always send a response in error handlers to avoid hanging requests.

Key Takeaways

Call next(error) to pass errors to Express error-handling middleware.
Define error handlers with four parameters: (err, req, res, next).
Place error-handling middleware after all routes and normal middleware.
Always send a response inside error handlers to end the request.
Avoid calling next() without an error inside error handlers to prevent loops.