express-no-try-catch
v1.0.1
Published
A simple Express middleware to safely handle async errors without try/catch everywhere.
Downloads
14
Maintainers
Readme
express-no-try-catch
A simple Express middleware to safely handle async errors without try/catch everywhere.
What This Package Does
This package solves a common problem in Express.js applications: the need to wrap every async route in try/catch blocks to prevent server crashes from unhandled promise rejections.
How It's Different From Existing Packages
Most existing packages either:
- Require global overrides that affect all Express behavior
- Have complex APIs with many options
- Include unnecessary dependencies
- Don't provide consistent error formatting
This package is different because:
- Simple API: Just wrap your async routes with
wrapAsync() - Zero Dependencies: Lightweight with no external packages
- Consistent Errors: Standard JSON error responses
- No Global Changes: Only affects routes you explicitly wrap
- Built-in Trace IDs: Automatic request tracking
Installation
npm install express-no-try-catchQuick Start
const express = require("express");
const { wrapAsync, errorHandler } = require("express-no-try-catch");
const app = express();
// Wrap your async routes with wrapAsync
app.get("/user/:id", wrapAsync(async (req, res) => {
const user = await getUserFromDB(req.params.id);
if (!user) {
const error = new Error("User not found");
error.status = 404;
throw error;
}
res.json(user);
}));
// Add error handler at the end
app.use(errorHandler);
app.listen(3000);How It Works
The Problem
Without this package, you need to wrap every async route in try/catch:
app.get("/user/:id", async (req, res, next) => {
try {
const user = await getUserFromDB(req.params.id);
res.json(user);
} catch (err) {
next(err); // Easy to forget!
}
});The Solution
With express-no-try-catch, errors are caught automatically:
app.get("/user/:id", wrapAsync(async (req, res) => {
const user = await getUserFromDB(req.params.id);
res.json(user);
// No try/catch needed!
}));API
wrapAsync(fn)
Wraps an async route handler to automatically catch errors.
const handler = wrapAsync(async (req, res) => {
// Your async code here
const result = await someAsyncOperation();
res.json(result);
});errorHandler(err, req, res, next)
Global error handler that formats errors as JSON.
app.use(errorHandler);Error responses are formatted as:
{
"success": false,
"message": "Error message",
"traceId": "request-id"
}Features
- Automatic error catching for async routes
- No try/catch boilerplate needed
- Consistent JSON error responses
- Built-in trace ID support
- Zero dependencies
- Lightweight (0.5KB)
Examples
Database Operations
app.get("/users/:id", wrapAsync(async (req, res) => {
const user = await User.findById(req.params.id);
if (!user) {
const error = new Error("User not found");
error.status = 404;
throw error;
}
res.json(user);
}));API Calls
app.post("/webhook", wrapAsync(async (req, res) => {
const result = await externalAPI.process(req.body);
res.json(result);
}));Custom Status Codes
app.post("/data", wrapAsync(async (req, res) => {
if (!req.body.name) {
const error = new Error("Name is required");
error.status = 400;
throw error;
}
res.json({ success: true });
}));License
MIT
Author
Maheshwari KM
