/** * Node-runtime-only side of the instrumentation hook. * * This module is loaded via dynamic `import()` from `instrumentation.ts` only * when `NEXT_RUNTIME === "nodejs"`. Keeping the `process.on` calls in a * separate file prevents Next.js's Edge Runtime static analyser from flagging * them — the runtime guard inside `register()` alone is not enough because * Next inspects every top-level reference in `instrumentation.ts` regardless * of control flow. * * The listeners only log: they never swallow the error, never mutate * response state, and never call `process.exit`. If an error is severe * enough to warrant termination, Node's default behaviour still applies on * subsequent throws. */ import { createLogger } from "@tablestore/tablestore-agent-shared"; const logger = createLogger("dashboard.process"); export function installNodeProcessListeners(): void { process.on("unhandledRejection", (reason) => { const message = reason instanceof Error ? (reason.stack ?? reason.message) : typeof reason === "string" ? reason : JSON.stringify(reason); logger.error({ event: "process.unhandled_rejection", message: reason instanceof Error ? reason.message : message, error: reason, }); console.error("[instrumentation] unhandledRejection:", message); }); process.on("uncaughtException", (err) => { logger.error({ event: "process.uncaught_exception", message: err.message, error: err, }); console.error( "[instrumentation] uncaughtException:", err.stack ?? err.message, ); }); }