File size: 1,101 Bytes
fae625a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8162097
 
 
 
 
 
 
 
fae625a
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
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,
	};
}