-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
48 lines (41 loc) · 1.43 KB
/
Copy pathinstrumentation.ts
File metadata and controls
48 lines (41 loc) · 1.43 KB
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
46
47
48
import type { Instrumentation } from 'next';
const LOG_PREFIX = '[fetch-url]';
const INIT_MESSAGE = `${LOG_PREFIX} Next.js server initializing...`;
const REQUEST_ERROR_TYPE = 'next-request-error';
function readRecord(value: unknown): Record<string, unknown> | undefined {
return typeof value === 'object' && value !== null
? (value as Record<string, unknown>)
: undefined;
}
function readString(value: unknown): string | undefined {
return typeof value === 'string' ? value : undefined;
}
export function register() {
if (process.env.NEXT_RUNTIME !== 'nodejs') {
return;
}
console.log(INIT_MESSAGE);
}
export const onRequestError: Instrumentation.onRequestError = (
error,
request,
context
) => {
const errorRecord = readRecord(error);
const requestRecord = readRecord(request);
const contextRecord = readRecord(context);
console.error(
JSON.stringify({
type: REQUEST_ERROR_TYPE,
digest: readString(errorRecord?.digest),
message: readString(errorRecord?.message) ?? 'Unknown request error',
method: readString(requestRecord?.method),
path: readString(requestRecord?.path),
routePath: readString(contextRecord?.routePath),
routeType: readString(contextRecord?.routeType),
routerKind: readString(contextRecord?.routerKind),
renderSource: readString(contextRecord?.renderSource),
revalidateReason: readString(contextRecord?.revalidateReason),
})
);
};