Files
sexy/instrumentation.ts
T
valknarandClaude Sonnet 5 9e0380ec75
CI / Build and push image (push) Successful in 1m10s
CI / Static checks (push) Successful in 1m39s
Add consistent API request logging and a themed 404 page, bump to 0.4.0
Every app/api route handler now goes through withRouteLogging, which logs
a correlated reqId/method/path/status/duration for each request and turns
any uncaught error into a logged stack trace plus a clean JSON 500 instead
of Next's default opaque failure. proxy.ts logs rejected auth attempts, and
instrumentation.ts's onRequestError catches anything that still escapes a
route handler. Also adds a minimal not-found page matching the app's card
styling.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-26 08:45:15 +02:00

34 lines
1.3 KiB
TypeScript

import { createLogger } from "@/lib/logger";
export async function register() {
// Only the Node.js server runtime touches better-sqlite3; the Edge
// middleware runtime must never import this module.
if (process.env.NEXT_RUNTIME === "nodejs") {
const { runMigrations } = await import("@/lib/db/migrate");
runMigrations();
}
}
export async function onRequestError(
error: unknown,
request: { path: string; method: string; headers: Record<string, string | string[]> },
context: { routerKind: string; routePath: string; routeType: string },
) {
const log = createLogger("uncaught");
// Safety net for errors that escape a route handler's own try/catch (e.g. a
// bug in code that never reaches withRouteLogging, or a rendering error) -
// route handlers wrapped in withRouteLogging already log and convert their
// own errors to a JSON 500, so this rarely double-logs the same failure.
const digest = typeof error === "object" && error !== null && "digest" in error ? String(error.digest) : undefined;
log.error("unhandled error", {
message: error instanceof Error ? error.message : String(error),
digest,
stack: error instanceof Error ? error.stack : undefined,
path: request.path,
method: request.method,
routePath: context.routePath,
routeType: context.routeType,
});
}