From 481c6a1627c62f68820be4c6b17d2c084e6d9430 Mon Sep 17 00:00:00 2001 From: Calum Muir Date: Sun, 28 Dec 2025 12:57:21 +0000 Subject: [PATCH] Return JSON on unhandled contact errors --- functions/api/contact.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/functions/api/contact.ts b/functions/api/contact.ts index ee6a731..c8a1faa 100644 --- a/functions/api/contact.ts +++ b/functions/api/contact.ts @@ -149,8 +149,20 @@ const handlePost: PagesFunction = async ({ request, env }) => { }; export const onRequest: PagesFunction = async (context) => { - if (context.request.method !== "POST") { - return jsonResponse({ ok: false, error: "Method not allowed." }, 405); + try { + if (context.request.method !== "POST") { + return jsonResponse( + { ok: false, error: "Method not allowed." }, + 405, + ); + } + return handlePost(context); + } catch (error) { + const message = + error instanceof Error ? error.message : "Unknown error"; + return jsonResponse( + { ok: false, error: "Unhandled exception.", details: message }, + 500, + ); } - return handlePost(context); };