-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureRequestError.ts
More file actions
47 lines (40 loc) · 1.34 KB
/
captureRequestError.ts
File metadata and controls
47 lines (40 loc) · 1.34 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
import type { RequestEventData } from '@sentry/core';
import { captureException, headersToDict, withScope } from '@sentry/core';
import { flushSafelyWithTimeout, waitUntil } from './utils/responseEnd';
type RequestInfo = {
path: string;
method: string;
headers: Record<string, string | string[] | undefined>;
};
type ErrorContext = {
routerKind: string; // 'Pages Router' | 'App Router'
routePath: string;
routeType: string; // 'render' | 'route' | 'middleware'
};
/**
* Reports errors passed to the the Next.js `onRequestError` instrumentation hook.
*/
export function captureRequestError(error: unknown, request: RequestInfo, errorContext: ErrorContext): void {
withScope(scope => {
scope.setSDKProcessingMetadata({
normalizedRequest: {
headers: headersToDict(request.headers),
method: request.method,
} satisfies RequestEventData,
});
scope.setContext('nextjs', {
request_path: request.path,
router_kind: errorContext.routerKind,
router_path: errorContext.routePath,
route_type: errorContext.routeType,
});
scope.setTransactionName(`${request.method} ${errorContext.routePath}`);
captureException(error, {
mechanism: {
handled: false,
type: 'auto.function.nextjs.on_request_error',
},
});
waitUntil(flushSafelyWithTimeout());
});
}