forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
58 lines (53 loc) · 1.83 KB
/
Copy pathinstrumentation.ts
File metadata and controls
58 lines (53 loc) · 1.83 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
/**
* Next.js Instrumentation
*
* This file runs once when the server starts and sets up global error handlers
* to catch unhandled promise rejections and uncaught exceptions.
*
* Without these handlers, unhandled errors can crash the Node.js process,
* causing Render's proxy to return 502 Bad Gateway errors.
*/
import { logger } from '@/util/logger'
export async function register() {
// Handle unhandled promise rejections (async errors that aren't caught)
process.on(
'unhandledRejection',
(reason: unknown, promise: Promise<unknown>) => {
logger.error(
{
reason:
reason instanceof Error
? { message: reason.message, stack: reason.stack }
: reason,
promise: String(promise),
},
'[CRITICAL] Unhandled Promise Rejection',
)
// Don't exit - let the process continue to handle other requests
// In production, Render will restart if there's a real crash
},
)
// Handle uncaught exceptions (sync errors that aren't caught)
process.on('uncaughtException', (error: Error, origin: string) => {
logger.error(
{
message: error.message,
stack: error.stack,
origin,
},
'[CRITICAL] Uncaught Exception',
)
// Don't exit - let the process continue to handle other requests
// This prevents a single bad request from taking down the entire server
})
logger.info({}, '[Instrumentation] Global error handlers registered')
// DB-touching admission module uses `postgres`, which imports Node built-ins
// like `crypto`. Gate on NEXT_RUNTIME so the edge bundle doesn't try to
// resolve them.
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { startFreeSessionAdmission } = await import(
'@/server/free-session/admission'
)
startFreeSessionAdmission()
}
}