Error Handling
Pattern
Wrap async route handlers with asyncHandler and throw AppError for HTTP errors:
import { asyncHandler, AppError } from '../middleware/error.middleware.js';
router.get('/resource/:id', asyncHandler(async (req, res) => {
const item = await findItem(req.params.id);
if (!item) throw new AppError('Resource not found', 404);
res.json({ success: true, data: item });
}));
Response Format
Success
{ "success": true, "data": { ... } }
Error
{ "success": false, "error": "Resource not found" }
How It Works
asyncHandlerwraps the async function in a try/catch- If the handler throws an
AppError, the error middleware sends the appropriate HTTP status and message - If an unexpected error occurs, the middleware sends a 500 with a generic message (no stack trace in production)