fix: log validation errors with field detail + raw body on event reject

validateBody now extracts per-field error messages from anyvali issues.
Event endpoint logs the raw body (first 500 chars) on validation failure
so we can see exactly what the kiosk sends.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mitchell R 2026-05-26 15:15:41 +02:00
parent 8381ed280e
commit 38c78c0bb5
No known key found for this signature in database
2 changed files with 18 additions and 4 deletions

View file

@ -626,7 +626,17 @@ function registerKioskRoutes(
const kiosk = await auth.verifyKioskKey(token);
if (!kiosk) throw createError({ statusCode: 401, statusMessage: "Invalid kiosk key" });
const body = validateBody(EventBody, await readBody(event));
const raw = await readBody(event);
let body: ReturnType<typeof EventBody["parse"]>;
try {
body = validateBody(EventBody, raw);
} catch (err: any) {
event.context.obs?.log.warn("event validation failed: {msg} body={raw}", {
msg: err.message ?? "unknown",
raw: JSON.stringify(raw).slice(0, 500),
});
throw err;
}
const payload = (body.payload ?? {}) as Record<string, unknown>;
event.context.obs?.log.info("event from kiosk {id} topic {topic}", { id: String(kiosk.id), topic: body.topic });

View file

@ -155,9 +155,13 @@ export const PasswordChangeBody = av.object(
export function validateBody<T>(schema: { safeParse(input: unknown): { success: boolean; data?: T; error?: unknown } }, raw: unknown): T {
const result = schema.safeParse(raw);
if (!result.success) {
const msg = typeof result.error === "object" && result.error && "message" in result.error
? String((result.error as any).message)
: "invalid request body";
let msg = "invalid request body";
const err = result.error as any;
if (err?.issues) {
msg = err.issues.map((i: any) => `${i.path?.join?.(".") ?? "?"}: ${i.message}`).join("; ");
} else if (err?.message) {
msg = String(err.message);
}
throw Object.assign(new Error(msg), { status: 400, statusText: "Bad Request" });
}
return result.data as T;