mirror of
https://github.com/BetterCorp/BetterFrame.git
synced 2026-05-26 16:56:33 +00:00
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:
parent
8381ed280e
commit
38c78c0bb5
2 changed files with 18 additions and 4 deletions
|
|
@ -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 });
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue