Return detailed ACS error responses

This commit is contained in:
Calum Muir 2025-12-22 13:49:23 +00:00
parent c50b6ea499
commit 0cfe7522fd

View file

@ -134,12 +134,24 @@ const handleContact = async (request: Request, env: Env) => {
},
);
if (!response.ok) {
const errorText = await response.text();
const contentType = response.headers.get("content-type") ?? "";
const errorText = contentType.includes("application/json")
? JSON.stringify(await response.json().catch(() => ({})))
: await response.text();
const errorCode = response.headers.get("x-ms-error-code");
const details = [
`${response.status} ${response.statusText}`.trim(),
errorCode ? `code=${errorCode}` : "",
errorText.trim(),
]
.filter(Boolean)
.join(" | ")
.slice(0, 500);
return jsonResponse(
{
ok: false,
error: "Failed to send email.",
details: errorText.slice(0, 500),
details,
},
502,
);