Email service surfaces real Resend errors instead of silent false
Deploy to route.crispygoat.com / deploy (push) Successful in 4m5s

The sendEmail / sendCampaignEmail / sendWelcomeEmail /
sendOrderReceiptEmail / sendPasswordResetEmail / sendOperationalAlert
helpers all returned Promise<boolean> and silently returned false on
any failure (missing API key, 401 invalid key, 422 unverified sender
domain, network error, etc.). The caller had no way to know which
problem it was.

Switch the public return type to a structured result:

  export type EmailSendResult = { ok: true } | { ok: false; error: string };

- Missing RESEND_API_KEY: error reads 'RESEND_API_KEY is not set.
  Add it to .env.local (or your hosting dashboard) and restart the
  server.'
- 4xx/5xx: error reads 'Resend 422 — validation_error: The gmail.com
  domain is not verified' (extracts Resend's own name + message).
- Network failure: error reads the thrown message verbatim.
- Non-JSON error body: falls back to 'Resend <status>'.

Callers updated:
  - src/actions/admin/users.ts (createAdminUser): now propagates the
    real emailError into the modal UI — no more generic 'sendWelcome
    Email returned false'.
  - src/actions/checkout.ts: logs the real error.
  - src/app/api/cron/send-scheduled/route.ts: per-recipient error
    logged for the scheduled-campaigns cron.

Tests:
  - tests/unit/email-service.test.ts (new, 5 tests): covers happy
    path, missing key, 4xx/5xx with JSON body, 4xx/5xx with non-JSON
    body, and network failure.
  - tests/unit/create-admin-user.test.ts: updated to use the new
    { ok, error? } return shape; new assertion that the action
    surfaces the real emailError string into the result.
This commit is contained in:
Tyler
2026-06-17 12:10:04 -06:00
parent 9d9bc5d257
commit 7e665ea43e
6 changed files with 190 additions and 18 deletions
+2 -2
View File
@@ -134,7 +134,7 @@ async function sendWelcomeEmailSafe(input: {
}
}
const ok = await sendWelcomeEmail({
const result = await sendWelcomeEmail({
to: input.to,
name: input.name,
role: emailRole as "brand_admin" | "wholesale_buyer" | "store_employee",
@@ -142,7 +142,7 @@ async function sendWelcomeEmailSafe(input: {
tempPassword: input.password,
logoUrl: logoUrl ?? undefined,
});
return ok ? { sent: true } : { sent: false, error: "sendWelcomeEmail returned false" };
return result.ok ? { sent: true } : { sent: false, error: result.error };
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error("[createAdminUser] Welcome email failed (non-fatal):", msg);
+4 -1
View File
@@ -128,7 +128,7 @@ export async function createOrder(
}
}
await sendOrderReceiptEmail({
const result = await sendOrderReceiptEmail({
customerName,
customerEmail,
orderId: data.id,
@@ -149,6 +149,9 @@ export async function createOrder(
brandName: "Tuxedo Corn",
logoUrl,
});
if (!result.ok) {
console.error("[checkout] Order receipt email failed:", result.error);
}
} catch {
// Email failure should not fail the order
}