Files
PaperClipAI/server/src/middleware/error-handler.ts
T
DottaandClaude Opus 4.6 2e7bf85e7a Fix 500 error logs still showing generic pino-http message
The previous fix (8151331) set res.err but pino-http wasn't picking it
up (likely Express 5 response object behavior). Switch to a custom
__errorContext property on the response that customErrorMessage and
customProps read directly, bypassing pino-http's unreliable res.err
check. Remove duplicate manual logger.error calls from the error
handler since pino-http now gets the full context.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 11:41:06 -06:00

56 lines
1.5 KiB
TypeScript

import type { Request, Response, NextFunction } from "express";
import { ZodError } from "zod";
import { HttpError } from "../errors.js";
export interface ErrorContext {
error: { message: string; stack?: string; name?: string; details?: unknown; raw?: unknown };
method: string;
url: string;
reqBody?: unknown;
reqParams?: unknown;
reqQuery?: unknown;
}
export function errorHandler(
err: unknown,
req: Request,
res: Response,
_next: NextFunction,
) {
if (err instanceof HttpError) {
if (err.status >= 500) {
(res as any).__errorContext = {
error: { message: err.message, stack: err.stack, name: err.name, details: err.details },
method: req.method,
url: req.originalUrl,
reqBody: req.body,
reqParams: req.params,
reqQuery: req.query,
} satisfies ErrorContext;
}
res.status(err.status).json({
error: err.message,
...(err.details ? { details: err.details } : {}),
});
return;
}
if (err instanceof ZodError) {
res.status(400).json({ error: "Validation error", details: err.errors });
return;
}
(res as any).__errorContext = {
error: err instanceof Error
? { message: err.message, stack: err.stack, name: err.name }
: { message: String(err), raw: err },
method: req.method,
url: req.originalUrl,
reqBody: req.body,
reqParams: req.params,
reqQuery: req.query,
} satisfies ErrorContext;
res.status(500).json({ error: "Internal server error" });
}