[PR #5721] feat: Support callback function in onAPIError's errorURL #6178

Open
opened 2026-03-13 12:50:12 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5721
Author: @GautamBytes
Created: 11/1/2025
Status: 🔄 Open

Base: canaryHead: feat/onapierror-callback


📝 Commits (10+)

  • 99f8d36 feat: support callback function in onAPIError.errorURL
  • af8da13 implement minor nits suggested by copilot
  • 003488c Use URLSearchParams for error redirects
  • ca930c7 Merge branch 'canary' into feat/onapierror-callback
  • 78e04de refactor(core): Centralize duplicated error redirect logic
  • 8e76645 solved conflicts
  • a82a284 fixed failing workflows
  • fefc29a lint fix
  • 4368525 Merge branch 'canary' into feat/onapierror-callback
  • c03929c fix: sso merge conflict

📊 Changes

15 files changed (+275 additions, -223 deletions)

View changed files

📝 packages/better-auth/src/api/routes/callback.ts (+35 -26)
📝 packages/better-auth/src/api/routes/error.ts (+1 -1)
📝 packages/better-auth/src/context/__snapshots__/create-context.test.ts.snap (+1 -0)
📝 packages/better-auth/src/context/create-context.ts (+33 -1)
📝 packages/better-auth/src/oauth2/link-account.ts (+4 -4)
📝 packages/better-auth/src/oauth2/state.ts (+21 -17)
📝 packages/better-auth/src/plugins/admin/admin.ts (+4 -6)
📝 packages/better-auth/src/plugins/generic-oauth/index.ts (+24 -28)
📝 packages/better-auth/src/plugins/oauth-proxy/index.ts (+7 -6)
📝 packages/better-auth/src/plugins/oidc-provider/authorize.ts (+45 -59)
📝 packages/core/src/types/context.ts (+24 -0)
📝 packages/core/src/types/init-options.ts (+16 -3)
📝 packages/sso/src/oidc.test.ts (+1 -1)
📝 packages/sso/src/routes/sso.ts (+55 -70)
📝 packages/telemetry/src/detectors/detect-auth-config.ts (+4 -1)

📄 Description

Closes #4566

This PR enhances error handling by allowing onAPIError.errorURL to be a function, not just a static string. This enables developers to dynamically generate redirect URLs based on the specific error.

The new function signature is: ({ error, error_description }) => string | Promise

All error-handling logic across OAuth, SSO, and state validation has been updated to check if errorURL is a function and await its result if it is.

This change is fully backward compatible with existing string-based configurations.


Summary by cubic

Adds support for a callback in onAPIError.errorURL so apps can redirect to dynamic error pages per error. String configs still work; we append error and error_description to the chosen URL.

  • New Features

    • errorURL can be a function: ({ error, error_description }) => string | Promise.
    • Applied across OAuth, SSO, admin, proxy, and OIDC flows; awaits async URLs.
    • Per-request state.errorURL (string) takes priority over global config; otherwise falls back to baseURL/error.
    • Consistent query param building for error and error_description.
    • Telemetry now indicates if errorURL is a function or present.
  • Refactors

    • Centralized error redirect handling via ctx.handleErrorRedirect, with optional overrideErrorURL and default path.

Written for commit 9a4f30160b. Summary will update automatically 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/5721 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 11/1/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/onapierror-callback` --- ### 📝 Commits (10+) - [`99f8d36`](https://github.com/better-auth/better-auth/commit/99f8d364cd10c9e984b8617cf0cc308291e32ac3) feat: support callback function in onAPIError.errorURL - [`af8da13`](https://github.com/better-auth/better-auth/commit/af8da13f8aa987c17d6fc7f9a05d2eeae3e720b4) implement minor nits suggested by copilot - [`003488c`](https://github.com/better-auth/better-auth/commit/003488c24d3eabed2ea143e93fc934df1cdd3072) Use URLSearchParams for error redirects - [`ca930c7`](https://github.com/better-auth/better-auth/commit/ca930c753d9911520e57c91ba5026335a92421d4) Merge branch 'canary' into feat/onapierror-callback - [`78e04de`](https://github.com/better-auth/better-auth/commit/78e04de6088fecba94b66f6ab69586541a14811d) refactor(core): Centralize duplicated error redirect logic - [`8e76645`](https://github.com/better-auth/better-auth/commit/8e76645259104286bf43b9c8e1881232f2de93ce) solved conflicts - [`a82a284`](https://github.com/better-auth/better-auth/commit/a82a28454327d1f2771df29d1c3a202ec74f9ae3) fixed failing workflows - [`fefc29a`](https://github.com/better-auth/better-auth/commit/fefc29a057ccba7b53095b8d6313f9adb94cc80f) lint fix - [`4368525`](https://github.com/better-auth/better-auth/commit/4368525b0697c1924585668657f0e09b95e993cb) Merge branch 'canary' into feat/onapierror-callback - [`c03929c`](https://github.com/better-auth/better-auth/commit/c03929cb875eaf8a1f34a9f39c89de81f359f1df) fix: sso merge conflict ### 📊 Changes **15 files changed** (+275 additions, -223 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/callback.ts` (+35 -26) 📝 `packages/better-auth/src/api/routes/error.ts` (+1 -1) 📝 `packages/better-auth/src/context/__snapshots__/create-context.test.ts.snap` (+1 -0) 📝 `packages/better-auth/src/context/create-context.ts` (+33 -1) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+4 -4) 📝 `packages/better-auth/src/oauth2/state.ts` (+21 -17) 📝 `packages/better-auth/src/plugins/admin/admin.ts` (+4 -6) 📝 `packages/better-auth/src/plugins/generic-oauth/index.ts` (+24 -28) 📝 `packages/better-auth/src/plugins/oauth-proxy/index.ts` (+7 -6) 📝 `packages/better-auth/src/plugins/oidc-provider/authorize.ts` (+45 -59) 📝 `packages/core/src/types/context.ts` (+24 -0) 📝 `packages/core/src/types/init-options.ts` (+16 -3) 📝 `packages/sso/src/oidc.test.ts` (+1 -1) 📝 `packages/sso/src/routes/sso.ts` (+55 -70) 📝 `packages/telemetry/src/detectors/detect-auth-config.ts` (+4 -1) </details> ### 📄 Description Closes #4566 This PR enhances error handling by allowing onAPIError.errorURL to be a function, not just a static string. This enables developers to dynamically generate redirect URLs based on the specific error. The new function signature is: ({ error, error_description }) => string | Promise<string> All error-handling logic across OAuth, SSO, and state validation has been updated to check if errorURL is a function and await its result if it is. This change is fully backward compatible with existing string-based configurations. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds support for a callback in onAPIError.errorURL so apps can redirect to dynamic error pages per error. String configs still work; we append error and error_description to the chosen URL. - **New Features** - errorURL can be a function: ({ error, error_description }) => string | Promise<string>. - Applied across OAuth, SSO, admin, proxy, and OIDC flows; awaits async URLs. - Per-request state.errorURL (string) takes priority over global config; otherwise falls back to baseURL/error. - Consistent query param building for error and error_description. - Telemetry now indicates if errorURL is a function or present. - **Refactors** - Centralized error redirect handling via ctx.handleErrorRedirect, with optional overrideErrorURL and default path. <sup>Written for commit 9a4f30160be68c1b38d401bddc1f05419693c0aa. Summary will update automatically 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-03-13 12:50:13 -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#6178