-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
59 lines (53 loc) · 2.04 KB
/
Copy pathinstrumentation.ts
File metadata and controls
59 lines (53 loc) · 2.04 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
49
50
51
52
53
54
55
56
57
58
59
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs' && process.env.NODE_ENV !== 'development') {
const { captureServerException } = await import('./lib/posthog-server')
// Register global error handler for uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error)
captureServerException(error, 'server', {
error_type: 'uncaught_exception',
environment: process.env.NODE_ENV,
})
})
// Register global error handler for unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
const error = reason instanceof Error ? reason : new Error(String(reason))
console.error('Unhandled Rejection at:', promise, 'reason:', reason)
captureServerException(error, 'server', {
error_type: 'unhandled_rejection',
environment: process.env.NODE_ENV,
promise: promise.toString(),
})
})
console.log('PostHog server-side error tracking initialized')
}
}
export async function onRequestError(
err: { digest?: string } & Error,
request: {
path?: string
headers?: { [key: string]: string | string[] | undefined }
},
context: {
routerKind?: 'Pages Router' | 'App Router'
routePath?: string
routeType?: 'render' | 'route' | 'action' | 'middleware'
}
) {
if (process.env.NEXT_RUNTIME === 'nodejs' && process.env.NODE_ENV !== 'development') {
const { captureServerException } = await import('./lib/posthog-server')
// Extract user information from headers if available
const sessionId = request.headers?.['x-session-id'] as string
const distinctId = request.headers?.['x-distinct-id'] as string || 'anonymous'
captureServerException(err, distinctId, {
error_type: 'request_error',
request_path: request.path,
route_kind: context.routerKind,
route_path: context.routePath,
route_type: context.routeType,
session_id: sessionId,
digest: err.digest,
environment: process.env.NODE_ENV,
})
}
}