[PR #8842] fix: rethrow phone sendOTP failures #16495

Closed
opened 2026-04-13 10:32:33 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/8842

State: closed
Merged: Yes


Summary

Fixes the phone-number/send-otp route so sendOTP failures are no longer silently swallowed when advanced.backgroundTasks.handler is not configured.

Closes #8834

Before this change, the route always returned 200 { message: "code sent" } even if the configured sendOTP callback threw. That made SMS delivery failures invisible to clients.

What changed

  • Updated POST /phone-number/send-otp to:
    • schedule OTP sending in the background when advanced.backgroundTasks.handler is configured
    • otherwise await sendOTP directly and let failures propagate through the normal error pipeline
  • Kept runInBackgroundOrAwait unchanged to avoid altering other flows that intentionally hide send failures

Behavior

  • Without backgroundTasks.handler:
    • sendOTP throwing now results in a 500 response
  • With backgroundTasks.handler:
    • behavior stays deferred/fire-and-forget and the route still returns success.

Summary by cubic

Fixes phone-number/send-otp error handling. Clients now get a 500 when no background handler is configured; background mode stays fire-and-forget and now safely handles handler failures.

  • Bug Fixes
    • Await sendOTP when no advanced.backgroundTasks.handler is set so failures return 500.
    • With a handler, run sendOTP via runInBackground and log failures from both the promise and the handler; still return { message: "code sent" }.
    • Added tests for non-background failures, background failures, and handler-throw cases.

Written for commit 40d0229e3e. Summary will update on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8842 **State:** closed **Merged:** Yes --- ## Summary Fixes the `phone-number/send-otp` route so `sendOTP` failures are no longer silently swallowed when `advanced.backgroundTasks.handler` is not configured. ## Closes #8834 Before this change, the route always returned `200 { message: "code sent" }` even if the configured `sendOTP` callback threw. That made SMS delivery failures invisible to clients. ## What changed - Updated `POST /phone-number/send-otp` to: - schedule OTP sending in the background when `advanced.backgroundTasks.handler` is configured - otherwise await `sendOTP` directly and let failures propagate through the normal error pipeline - Kept `runInBackgroundOrAwait` unchanged to avoid altering other flows that intentionally hide send failures ## Behavior - Without `backgroundTasks.handler`: - `sendOTP` throwing now results in a `500` response - With `backgroundTasks.handler`: - behavior stays deferred/fire-and-forget and the route still returns success. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes `phone-number/send-otp` error handling. Clients now get a 500 when no background handler is configured; background mode stays fire-and-forget and now safely handles handler failures. - **Bug Fixes** - Await `sendOTP` when no `advanced.backgroundTasks.handler` is set so failures return 500. - With a handler, run `sendOTP` via `runInBackground` and log failures from both the promise and the handler; still return `{ message: "code sent" }`. - Added tests for non-background failures, background failures, and handler-throw cases. <sup>Written for commit 40d0229e3ef918527357c332de3ebadcba74986a. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 10:32:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16495