mirror of
https://github.com/Sea-Haven-Industries/seahaven-ap.git
synced 2026-10-05 09:31:58 +00:00
Move liveness to GET /api/health { stage, sha } with a Node 24 image on 8080 so ALB probes and deploy verify do not need auth or a database ping.
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import type { Context } from "hono";
|
|
import type { ContentfulStatusCode } from "hono/utils/http-status";
|
|
|
|
export type ErrorCode =
|
|
| "UNAUTHENTICATED"
|
|
| "FORBIDDEN"
|
|
| "NOT_FOUND"
|
|
| "VALIDATION_ERROR"
|
|
| "CONFLICT"
|
|
| "INTERNAL_ERROR"
|
|
| "DATABASE_UNAVAILABLE";
|
|
|
|
export const CORRELATION_HEADER = "x-correlation-id";
|
|
|
|
export type ErrorEnvelope = {
|
|
error: {
|
|
code: ErrorCode;
|
|
message: string;
|
|
correlationId: string;
|
|
};
|
|
};
|
|
|
|
export function correlationIdFrom(c: Context): string {
|
|
const raw = c.req.header(CORRELATION_HEADER)?.trim();
|
|
if (raw && raw.length <= 128) {
|
|
return raw;
|
|
}
|
|
return randomUUID();
|
|
}
|
|
|
|
export function errorBody(code: ErrorCode, message: string, correlationId: string): ErrorEnvelope {
|
|
return { error: { code, message, correlationId } };
|
|
}
|
|
|
|
export function errorJson(
|
|
c: Context,
|
|
status: ContentfulStatusCode,
|
|
code: ErrorCode,
|
|
message: string,
|
|
correlationId?: string,
|
|
) {
|
|
const id = correlationId ?? correlationIdFrom(c);
|
|
c.header(CORRELATION_HEADER, id);
|
|
return c.json(errorBody(code, message, id), status);
|
|
}
|