fix(mailer,audit): propagate send and write failures so caller context logging fires#3973
Conversation
…gging fires lib/helpers/mailer/index.js#sendMail and modules/audit/services/audit.service.js#log each wrapped their failing call in a local try/catch, logged one generic line, and resolved null instead of rejecting. Every caller's own context-rich .catch() (action/userId/orgId/targetType) never ran on a real outage — dead code hidden behind a swallowed rejection. Let both helpers propagate the underlying rejection. The "mail/audit failure never breaks the main flow" invariant now lives at the call sites (their existing .catch()), not centrally in the helper: - auth signup/reset, billing.email.js, invitations.service.js, and the 4 organizations.membership.service.js sites already had context-logging .catch() handlers that were previously unreachable — now they fire. - invitations.service.js#resend already documented "a transport failure surfaces as an error" but the swallow silently defeated that; propagation fixes it to match its own doc comment. - audit.middleware.js's .catch() is enriched with action/userId/organizationId/ targetType/targetId so a write failure now logs full request context, not just message+stack. Closes #3966 Claude-Session: https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup
… failures resendVerification (auth.controller.js) and the invitations resend endpoint now surface propagated mail-transport failures via errors.getMessage(err), which relayed the raw SMTP/Resend provider error string to the client on these two endpoints — a low-severity infra-detail leak flagged by the pre-push critical-review gate on #3966. Both catches now log the real error server-side with context (userId / invitationId) and respond with a stable generic message. invitations.resend still threads deliberate AppError statuses (409 conflict, 404 not found, 422 mailer-not-configured) through unchanged — only the untagged transport rejection (no `.status`) gets the generic treatment. resendVerification's catch is resend-email-scoped end-to-end, so the whole catch goes generic. Claude-Session: https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup
|
Warning Review limit reached
Next review available in: 29 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (12)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3973 +/- ##
==========================================
+ Coverage 93.06% 93.13% +0.07%
==========================================
Files 170 170
Lines 5595 5596 +1
Branches 1802 1803 +1
==========================================
+ Hits 5207 5212 +5
+ Misses 313 309 -4
Partials 75 75
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
|
@coderabbitai full review |
✅ Action performedFull review finished. Your plan includes PR reviews subject to rate limits. More reviews will be available in 29 minutes. |
Summary
lib/helpers/mailer/index.js#sendMailandmodules/audit/services/audit.service.js#logeach wrapped their failing call in a local try/catch, logged one generic line, and resolvednullinstead of rejecting. Every caller's own context-rich.catch()(action/userId/orgId/targetType) never ran on a real outage — dead code hidden behind a swallowed rejection. Both helpers now propagate the underlying rejection;audit.middleware.js's.catch()is enriched with action/userId/organizationId/targetType/targetId so a write failure now logs full request context, not just message+stack.invitations.service.js#resendalready documented "a transport failure surfaces as an error" but the swallow silently defeated that — propagation fixes it to match its own doc comment. On top of the propagation,resendVerification(auth) and the invitationsresendendpoint now return a stable generic client message on a mail-transport failure instead of the raw provider error string — the real error is still logged server-side with context..catch()dead code. Propagating moves that invariant to the call sites, which already carry (or, for the two resend endpoints, now carry) the right catch/logging. Propagating also surfaced a pre-existing leak:errors.getMessage(err)on those two endpoints relayed the raw SMTP/Resend provider error straight to the client (flagged by the pre-push critical-review gate) — fixed by giving those two catches a generic client message while still logging the real error server-side.Scope
lib/helpers/mailer,modules/audit(service + middleware),modules/auth/controllers,modules/invitations/controllersmails.sendMail()andAuditService.log()now sees a rejecting promise instead ofnull. Audited: auth signup/reset,billing.email.js, invitations service (fire-and-forgetcreate()path),organizations.membership.service.js(×4 sites), users email-change — all already carry their own context.catch(), so behavior is unaffected beyond that catch now actually running.resendVerificationandinvitations.resendwere the two awaited (non-fire-and-forget) call sites without a local.catch()— hardened directly in this PR to log server-side and respond generically instead of relaying the raw error.Validation
npm run lintnpm test(unit — 2189/2189 green, including new coverage for both hardened catches)Tests assert: a provider/repo rejection reaches the caller's context
.catch()(mailer + audit unit tests), the main flow never crashes on a mail/audit failure, and — new in this PR — the client response for a mail-transport failure onresendVerification/invitations.resenddoes not contain the raw provider error string while the server-side context log still fires.Guardrails check
.env*,secrets/**, keys, tokens)Notes for reviewers
resendVerificationandinvitations.resendpreviously relayed the raw SMTP/Resend provider error string to the client viaerrors.getMessage(err). Both now log the real error server-side with context (userId / invitationId) and respond with a stable generic message ("Failed to send the email, please try again.").invitations.resendstill threads deliberateAppErrorstatuses (409 conflict, 404 not found, 422 mailer-not-configured) through unchanged — only the untagged transport rejection (no.status) gets the generic treatment.resendVerification/invitations.resendnow return 422 instead of the old misleading 400/200 on some previously-swallowed failure paths — intentional, more-correct status, not a regression.https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup