Mike0021's picture
fix: keep error logging process-safe
8162097 verified
Raw History Blame Contribute Delete
1.1 kB
import type { HandleServerError } from "@sveltejs/kit";
import { logger } from "$lib/server/logger";
type HandleServerErrorInput = Parameters<HandleServerError>[0];
export async function handleServerError({
error,
event,
status,
message,
}: HandleServerErrorInput): Promise<App.Error> {
// handle 404
if (event.route.id === null) {
return {
message: `Page ${event.url.pathname} not found`,
};
}
const errorId = crypto.randomUUID();
logger.error({
locals: event.locals,
url: event.request.url,
params: event.params,
// Native Request/Headers instances contain private Undici accessors. Passing
// them through Pino's path redactor can invoke those accessors with the wrong
// receiver and crash the process while handling the original error. Keep the
// diagnostic fields useful and deliberately omit request headers/body.
request: {
method: event.request.method,
mode: event.request.mode,
},
message,
error,
errorId,
status,
stack: error instanceof Error ? error.stack : undefined,
});
return {
message: "An error occurred",
errorId,
};
}