Spaces:
Running
Running
| 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) | |
| } | |
| }); | |
| } | |