From cb3ffb6593364ee6ff56b17cd97869049687e6c9 Mon Sep 17 00:00:00 2001 From: Vinicius Garcia Date: Fri, 9 Oct 2026 13:28:58 +0000 Subject: [PATCH] fix(auth): logar o motivo da recusa do Resend e a causa de fetch failed Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01VDxbrneS3TEAUmdgVmkKpQ --- src/server/auth/auth.ts | 2 +- src/server/auth/mail.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/auth/auth.ts b/src/server/auth/auth.ts index 3bdf81f..12946a2 100644 --- a/src/server/auth/auth.ts +++ b/src/server/auth/auth.ts @@ -75,7 +75,7 @@ export function createAuth({ // Not awaited on purpose (timing attacks), but kept alive with after(): on Vercel the function can be frozen // as soon as the response is sent, which silently drops a fire-and-forget fetch. Errors never include the body. const task = send({ to: email, subject: "Seu código de acesso ao Compasso", text: `Seu código: ${otp}\nVálido por 5 minutos.` }).catch( - (e: unknown) => console.error("[auth] failed to send sign-in code", e instanceof Error ? e.message : e), + (e: unknown) => console.error("[auth] failed to send sign-in code", e instanceof Error ? `${e.message}${e.cause ? ` (cause: ${String(e.cause)})` : ""}` : e), ); try { after(task); diff --git a/src/server/auth/mail.ts b/src/server/auth/mail.ts index f038a07..45b90b6 100644 --- a/src/server/auth/mail.ts +++ b/src/server/auth/mail.ts @@ -19,5 +19,9 @@ export const sendMail: Mailer = async (message) => { headers: { Authorization: `Bearer ${env.RESEND_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ from: env.MAIL_FROM, ...message }), }); - if (!res.ok) throw new Error(`Resend responded ${res.status}`); + if (!res.ok) { + // Resend's error body is {name, message} (e.g. "Invalid `from` field"): useful and free of secrets/OTPs. + const detail = await res.json().then((b: { name?: string; message?: string }) => `${b.name ?? ""}: ${b.message ?? ""}`).catch(() => ""); + throw new Error(`Resend responded ${res.status} ${detail}`.trim()); + } };