-
-
Notifications
You must be signed in to change notification settings - Fork 21.7k
Open
Description
Description
Many developers struggle with properly handling errors in async/await Express routes. A dedicated section in the documentation covering this pattern would be very helpful.
Current Situation
The error handling documentation primarily covers synchronous patterns. With async/await being the standard for modern Node.js applications, explicit guidance would help prevent unhandled promise rejections.
Suggested Documentation Addition
A section covering:
// Wrapper function for async routes
const asyncHandler = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
// Usage example
app.get('/users/:id', asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
}));Benefits
- Reduces common errors for Express beginners
- Promotes proper error handling patterns
- Prevents unhandled promise rejections in production
I'd be happy to contribute a PR for this documentation update if there's interest.
Metadata
Metadata
Assignees
Labels
No labels