Spaces:
Sleeping
Sleeping
Download backend/src/middleware/errors.ts from build-small-hackathon/lovegpt: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/build-small-hackathon/lovegpt/resolve/main/backend/src/middleware/errors.ts
- Command line
-
hf download hf://spaces/build-small-hackathon/lovegpt/backend/src/middleware/errors.ts
-
curl -L -o errors.ts https://huggingface.co/spaces/build-small-hackathon/lovegpt/resolve/main/backend/src/middleware/errors.ts
1.05 kB
| 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) | |
| } | |
| }); | |
| } | |