fix(donations/dropoff): catch resolved-failure from sendCustomEmail (self-inflicted swallow in 2eed4963)

2eed4963 shipped the dropoff route with bare `.catch()` wrappers around
both sendCustomEmail calls. Per the helper's contract (and per the
swallow-fix sweep that closed 11+ instances of this pattern earlier in
this thread), sendCustomEmail RESOLVES `{success:false}` on SMTP /
Listmonk failure rather than throwing — the bare `.catch()` only fires
on actual rejections, so the most common failure mode (Listmonk
returns 5xx) would slip through silently and the donor would see a
success message while the confirmation email never arrived.

Replace each bare-catch with the canonical pattern from
src/lib/it-hilfe/notifications.ts: a `.then(r => if !r.success log)`
chained with a `.catch(err => log)` so both modes are caught and
distinguishable in logs by the `(resolved)` vs `(rejected)` suffix.

Adds one regression-lock test that mockResolvedValues both sends with
`{success:false}` and asserts logger.warn fires with `(resolved)` —
locks the .then() pattern so a future refactor can't silently revert
to bare-catch. 10/10 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-31 03:28:18 +02:00
1 parent b95a57761c
commit bedc4e8e30
2 files changed
+43 -8

No files matched your search

@@ -143,4 +143,25 @@ describe('POST /api/donations/dropoff', () => {
const res = await POST(makeRequest(VALID_BODY))
expect(res.status).toBe(200)
})
it('still returns 200 when sendCustomEmail resolves { success: false } — but logs the resolved failure', async () => {
// sendCustomEmail RESOLVES with {success:false} on SMTP failure rather
// than throwing. A bare `.catch()` would miss this — the route uses a
// `.then()` to detect resolved-failure and log it. Without this lock
// a refactor could silently revert to the bare-catch shape (the bug
// I just shipped in a8cc473c and fixed adjacent).
const { logger } = jest.requireMock('@/lib/logger') as { logger: { warn: jest.Mock } }
mockSendCustomEmail.mockResolvedValue({ success: false, error: 'Listmonk 500' })
const res = await POST(makeRequest(VALID_BODY))
expect(res.status).toBe(200)
// Give the fire-and-forget .then() chain a tick to flush
await new Promise(r => setImmediate(r))
const resolvedFailureLogs = logger.warn.mock.calls.filter(
(call: unknown[]) => typeof call[0] === 'string' && call[0].includes('(resolved)'),
)
expect(resolvedFailureLogs.length).toBeGreaterThanOrEqual(2)
})
})
+22 -8
View File
@@ -44,15 +44,29 @@ export async function POST(request: NextRequest) {
// Both emails fire-and-forget so a transient SMTP failure doesn't
// surface as a 500 to a donor who just filled out the form correctly.
sendCustomEmail(
CONTACT.email,
donationDropoffNotification(fields),
).catch(err => logger.warn('Failed to send donation drop-off notification', { error: err, donorEmail: fields.email }))
//
// sendCustomEmail() resolves `{ success: false, error }` on SMTP /
// Listmonk failure rather than throwing — a bare `.catch()` only
// catches actual rejections, so resolved-failures slip through
// silently. Inspect each result in a `.then()` and log the resolved
// failure mode separately so ops can grep for both shapes. Same
// pattern as src/lib/it-hilfe/notifications.ts and the 11+ prior
// swallow fixes in this codebase.
sendCustomEmail(CONTACT.email, donationDropoffNotification(fields))
.then(r => {
if (!r.success) {
logger.warn('Failed to send donation drop-off notification (resolved)', { error: r.error, donorEmail: fields.email })
}
})
.catch(err => logger.warn('Failed to send donation drop-off notification (rejected)', { error: err, donorEmail: fields.email }))
sendCustomEmail(
fields.email,
donationDropoffConfirmation(fields),
).catch(err => logger.warn('Failed to send donation drop-off confirmation', { error: err, donorEmail: fields.email }))
sendCustomEmail(fields.email, donationDropoffConfirmation(fields))
.then(r => {
if (!r.success) {
logger.warn('Failed to send donation drop-off confirmation (resolved)', { error: r.error, donorEmail: fields.email })
}
})
.catch(err => logger.warn('Failed to send donation drop-off confirmation (rejected)', { error: err, donorEmail: fields.email }))
logger.info('Donation drop-off announced', { donorEmail: fields.email, hasPreferredDate: !!fields.preferredDate })