Bug: ReferenceError err is not defined in asyncHandler middleware

Author: rajvircodesCreated Jul 31, 2026Updated Jul 31, 2026

Describe the bug

In the asyncHandler utility middleware, there is a naming mismatch in the catch block. The caught variable is named error, but the response properties are being read from err. This causes a ReferenceError: err is not defined and crashes the server when an error is thrown.

Code Location

javascript
const asyncHandler = (fn) => async (req, res, next) => {
  try {
    await fn(req, res, next);
  } catch (error) { // <--- Named 'error'
    res.status(err.code || 500).json({ // <--- Using 'err'
      success: false,
      message: err.message,
    });
  }
};

Expected Behavior

The catch block should use the same variable name: catch (err) or access error.code and error.message.

Source: hiteshchoudhary/chai-backend