Express Error Handling Typed Middleware
Centralize Express error handling in TypeScript with a typed AppError hierarchy, status mapping, and an exhaustively narrowed middleware.
Code
import express, { NextFunction, Request, Response } from 'express';
type ErrorKind = 'validation' | 'not-found' | 'conflict' | 'internal';
class AppError extends Error {
constructor(
readonly kind: ErrorKind,
message: string,
readonly status = 500,
) {
super(message);
}
}
const statusFor = (kind: ErrorKind): number => {
switch (kind) {
case 'validation': return 400;
case 'not-found': return 404;
case 'conflict': return 409;
case 'internal': return 500;
}
};
const wrap = (fn: (req: Request, res: Response) => Promise<unknown>) =>
(req: Request, res: Response, next: NextFunction) => {
fn(req, res).catch(next);
};
const app = express();
app.use(express.json());
app.get('/items/:id', wrap(async (req, res) => {
if (req.params.id === '0') {
throw new AppError('not-found', 'item 0 does not exist');
}
res.json({ id: req.params.id });
}));
app.use((err: unknown, req: Request, res: Response, next: NextFunction) => {
if (!(err instanceof AppError)) {
console.error('unexpected:', err);
res.status(500).json({ error: 'internal error' });
return;
}
const status = statusFor(err.kind);
if (status >= 500) console.error('server error:', err.message);
res.status(status).json({ error: err.message, kind: err.kind });
});
app.listen(3000);How it works
Express Error Handling Typed Middleware is built around the class AppError and the functions statusFor and wrap.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. A guard precedes the AppError: bad state stops the flow there instead of continuing.
What to watch for
- Plan for an install step:
expressis a real dependency, resolved by the package manager. - The guard
req.params.id === '0'short-circuits the flow with an earlythrowwhen its condition holds.
Using it
The primary signature is const statusFor = (kind: ErrorKind)
The run emits 2 labeled sections — unexpected:, server error: — one per step.
Key statements
throw new AppError('not-found', 'item 0 does not exist');
(req: Request, res: Response, next: NextFunction) => {
fn(req, res).catch(next);
};
const statusFor = (kind: ErrorKind): number => {
switch (kind) {
case 'validation': return 400;
case 'not-found': return 404;
case 'conflict': return 409;
case 'internal': return 500;
}
};
Interactive Tools for This Topic
FAQ
- How does the snippet fail?
- It throws `AppError` before any real work runs, via the guard `req.params.id === '0'` — handle it at the call site or let it propagate deliberately.
- Are there third-party dependencies?
- `express` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.