[PR #8842] [MERGED] fix: rethrow phone sendOTP failures #25149

Closed
opened 2026-04-15 22:44:29 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8842
Author: @GautamBytes
Created: 3/30/2026
Status: Merged
Merged: 3/31/2026
Merged by: @ping-maxwell

Base: mainHead: fix/rethrow-send-otp-errors


📝 Commits (3)

  • 0ae6d57 Fix phone OTP error propagation
  • 78b357a fix: handle phone OTP background task failures
  • 40d0229 Merge branch 'main' into fix/rethrow-send-otp-errors

📊 Changes

2 files changed (+131 additions, -8 deletions)

View changed files

📝 packages/better-auth/src/plugins/phone-number/phone-number.test.ts (+109 -0)
📝 packages/better-auth/src/plugins/phone-number/routes.ts (+22 -8)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8842 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 3/30/2026 **Status:** ✅ Merged **Merged:** 3/31/2026 **Merged by:** [@ping-maxwell](https://github.com/ping-maxwell) **Base:** `main` ← **Head:** `fix/rethrow-send-otp-errors` --- ### 📝 Commits (3) - [`0ae6d57`](https://github.com/better-auth/better-auth/commit/0ae6d57ac048b4625ab40c4ed2790a2ac06dd8c4) Fix phone OTP error propagation - [`78b357a`](https://github.com/better-auth/better-auth/commit/78b357ade58ec580f9250656d0e79a2a56df868e) fix: handle phone OTP background task failures - [`40d0229`](https://github.com/better-auth/better-auth/commit/40d0229e3ef918527357c332de3ebadcba74986a) Merge branch 'main' into fix/rethrow-send-otp-errors ### 📊 Changes **2 files changed** (+131 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/phone-number/phone-number.test.ts` (+109 -0) 📝 `packages/better-auth/src/plugins/phone-number/routes.ts` (+22 -8) </details> ### 📄 Description ## 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. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:44:29 -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#25149