Files
OmniRoute/src/lib/api/errorResponse.ts
zhang-qiang aae2399631 fix(perf): resolve HMR singleton leaks, Edge warnings, and test stability
- Use globalThis singleton guards for DB connection, HealthCheck timers, console interceptor, and graceful shutdown to survive Webpack HMR re-evaluation (fixes 485+ leaked DB connections per session)

- Split instrumentation.ts into instrumentation-node.ts with computed import path to prevent Turbopack Edge bundler from tracing Node.js modules (eliminates 10+ spurious warnings per hot compile)

- Parallelize startup imports in instrumentation-node.ts (3 batch Promise.all instead of 9 serial awaits)

- Add OMNIROUTE_USE_TURBOPACK=1 env switch in run-next.mjs (default behavior unchanged)

- Replace node:crypto with crypto in proxies.ts and errorResponse.ts to fix UnhandledSchemeError

- Add unlinkFileWithRetry with EBUSY/EPERM retry for Windows file handle timing in backup restore

- Fix pre-restore backup to await completion before closing DB

- Fix bootstrap-env, domain-persistence, and fixes-p1 test stability on Windows

Made-with: Cursor
2026-03-21 00:50:07 +08:00

55 lines
1.3 KiB
TypeScript

import { randomUUID } from "crypto";
export type ApiErrorType = "invalid_request" | "not_found" | "conflict" | "server_error";
interface ApiErrorPayload {
status: number;
message: string;
type?: ApiErrorType;
details?: unknown;
}
export function createErrorResponse(payload: ApiErrorPayload): Response {
const requestId = randomUUID();
const resolvedType =
payload.type ||
(payload.status >= 500
? "server_error"
: payload.status === 404
? "not_found"
: payload.status === 409
? "conflict"
: "invalid_request");
return Response.json(
{
error: {
message: payload.message,
type: resolvedType,
details: payload.details,
},
requestId,
},
{ status: payload.status }
);
}
export function createErrorResponseFromUnknown(
error: unknown,
fallbackMessage = "Unexpected server error"
): Response {
const anyError = error as {
message?: string;
status?: number;
type?: ApiErrorType;
details?: unknown;
};
const status = Number(anyError?.status) || 500;
return createErrorResponse({
status,
message: typeof anyError?.message === "string" ? anyError.message : fallbackMessage,
type: anyError?.type,
details: anyError?.details,
});
}