Spaces:
Sleeping
Sleeping
File size: 1,049 Bytes
c536472 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import type { NextFunction, Request, Response } from "express";
import { ZodError } from "zod";
import { isProduction } from "../config/env";
import { HttpError, getErrorMessage } from "../utils/httpError";
export function notFoundHandler(req: Request, _res: Response, next: NextFunction): void {
next(new HttpError(404, "not_found", `Route not found: ${req.method} ${req.path}`));
}
export function errorHandler(error: unknown, _req: Request, res: Response, _next: NextFunction): void {
if (error instanceof ZodError) {
res.status(400).json({
error: {
code: "validation_error",
message: "Request validation failed",
issues: error.issues
}
});
return;
}
if (error instanceof HttpError) {
res.status(error.status).json({
error: {
code: error.code,
message: error.message
}
});
return;
}
res.status(500).json({
error: {
code: "internal_error",
message: isProduction ? "Internal server error" : getErrorMessage(error)
}
});
}
|