Files
photoreport/apps/api/src/lib/clientErrors.ts
T
sparkandClaude Opus 5.5 64d52fc691
ci / rust (push) Successful in 15m6s
ci / typescript (push) Successful in 18m21s
Errors to errors.bitreport.pt from API, worker, app, desktop, sites and print agent
API and worker report error log lines, unhandled 5xx and crashes to the hub with the
photoreport ingest key (ERROR_REPORTING_TOKEN); browsers (app, desktop webview, sites),
Next server renders and the print agent send through POST /v1/client-errors so the key
stays on the server. Repeats folded, rate-limited. docs/errors.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-14 19:32:54 +01:00

43 lines
1.7 KiB
TypeScript

import type { FastifyInstance } from "fastify";
import { z } from "zod";
import type { ErrorReporter } from "./reporter.js";
const ClientEvent = z.object({
message: z.string().min(1).max(2000),
stack: z.string().max(20_000).optional(),
level: z.enum(["error", "warning", "info"]).default("error"),
/** The web app, the public sites, the desktop shell's webview. */
component: z.enum(["app", "site", "site-server", "desktop", "print-agent"]),
url: z.string().max(500).optional(),
release: z.string().max(64).optional(),
userAgent: z.string().max(300).optional(),
extra: z.record(z.string(), z.union([z.string().max(500), z.number(), z.boolean(), z.null()])).optional(),
});
const Body = z.object({ events: z.array(ClientEvent).min(1).max(10) });
/**
* Errors from the browsers (app, sites, desktop webview), passed on to errors.bitreport.pt with the server's key:
* the key never reaches a browser. Rate-limited per address; the reporter folds repeats together.
*/
export function registerClientErrors(app: FastifyInstance, reporter: ErrorReporter): void {
app.post("/v1/client-errors", { config: { rateLimit: { max: 30, timeWindow: "1 minute" } }, bodyLimit: 256 * 1024 }, async (req, reply) => {
const { events } = Body.parse(req.body);
for (const e of events) {
reporter.report({
level: e.level,
message: e.message,
stack: e.stack,
component: e.component,
context: {
...(e.url && { client_url: e.url }),
...(e.release && { client_release: e.release }),
...(e.userAgent && { user_agent: e.userAgent }),
...e.extra,
},
});
}
return reply.code(202).send({ accepted: events.length });
});
}