From bedc4e8e30c91e61d741925d26856c52316a642f Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Sun, 31 May 2026 03:28:18 +0200 Subject: [PATCH] fix(donations/dropoff): catch resolved-failure from sendCustomEmail (self-inflicted swallow in 2eed4963) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../donations/dropoff/__tests__/route.test.ts | 21 +++++++++++++ src/app/api/donations/dropoff/route.ts | 30 ++++++++++++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/app/api/donations/dropoff/__tests__/route.test.ts b/src/app/api/donations/dropoff/__tests__/route.test.ts index 8f49025fe..c392ed323 100644 --- a/src/app/api/donations/dropoff/__tests__/route.test.ts +++ b/src/app/api/donations/dropoff/__tests__/route.test.ts @@ -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) + }) }) diff --git a/src/app/api/donations/dropoff/route.ts b/src/app/api/donations/dropoff/route.ts index ab1a820f1..5dfcb969a 100644 --- a/src/app/api/donations/dropoff/route.ts +++ b/src/app/api/donations/dropoff/route.ts @@ -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 })