[PR #5721] feat: support callback function in onAPIError's errorURL #14425

Open
opened 2026-04-13 09:28:09 -05:00 by GiteaMirror · 0 comments
Owner

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

State: open
Merged: No


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.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5721 **State:** open **Merged:** No --- 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. -->
GiteaMirror added the pull-request label 2026-04-13 09:28:09 -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#14425