[PR #6633] [MERGED] feat: add code property for api error instance #23696

Closed
opened 2026-04-15 21:55:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6633
Author: @himself65
Created: 12/9/2025
Status: Merged
Merged: 12/23/2025
Merged by: @himself65

Base: canaryHead: himself65/2025/12/09/error-code


📝 Commits (10+)

📊 Changes

100 files changed (+2072 additions, -1757 deletions)

View changed files

📝 docs/content/docs/concepts/api.mdx (+2 -2)
📝 packages/better-auth/src/api/index.ts (+6 -4)
📝 packages/better-auth/src/api/middlewares/origin-check.ts (+65 -5)
📝 packages/better-auth/src/api/routes/account.test.ts (+1 -1)
📝 packages/better-auth/src/api/routes/account.ts (+52 -52)
📝 packages/better-auth/src/api/routes/email-verification.ts (+30 -30)
📝 packages/better-auth/src/api/routes/reset-password.ts (+7 -15)
📝 packages/better-auth/src/api/routes/session.ts (+35 -15)
📝 packages/better-auth/src/api/routes/sign-in.test.ts (+3 -8)
📝 packages/better-auth/src/api/routes/sign-in.ts (+42 -43)
📝 packages/better-auth/src/api/routes/sign-up.ts (+34 -28)
📝 packages/better-auth/src/api/routes/update-user.ts (+45 -54)
📝 packages/better-auth/src/api/to-auth-endpoints.test.ts (+8 -7)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+7 -6)
📝 packages/better-auth/src/auth/auth.test.ts (+8 -6)
📝 packages/better-auth/src/call.test.ts (+8 -7)
📝 packages/better-auth/src/client/client.test.ts (+8 -8)
📝 packages/better-auth/src/client/types.ts (+7 -1)
📝 packages/better-auth/src/db/schema.ts (+11 -7)
📝 packages/better-auth/src/oauth2/link-account.ts (+3 -2)

...and 80 more files

📄 Description

Fixes: https://github.com/better-auth/better-auth/issues/6938


Summary by cubic

Standardized API error responses to always include a machine-readable code, and surfaced plugin error codes to clients for consistent handling.

  • New Features

    • Introduced APIError in @better-auth/core/error with from and fromStatus helpers.
    • Added isAPIError utility and updated docs to use it for safe error detection.
    • defineErrorCodes now returns { code, message }; BASE_ERROR_CODES expanded (origin/callback/redirect, email verification, sessions, etc.).
    • Updated all routes and plugins to return consistent { code, message } errors.
    • Origin/callback/redirect/errorCallbackURL/newUserCallbackURL validation now throws specific error codes.
    • Exposed error codes to clients via $ERROR_CODES across core plugins (admin, anonymous, api-key, email-otp, generic-oauth, multi-session, organization, phone-number, two-factor, username), plus Stripe and Passkey.
  • Migration

    • Client code should read error.code alongside error.message in responses.
    • Use isAPIError instead of instanceof checks when handling errors.
    • Custom plugins: update $ERROR_CODES to the new shape { code, message } and use defineErrorCodes accordingly.
    • When throwing errors in custom code, use APIError.from(status, errorCode) or APIError.fromStatus(status).

Written for commit 7c39df9b61. 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/6633 **Author:** [@himself65](https://github.com/himself65) **Created:** 12/9/2025 **Status:** ✅ Merged **Merged:** 12/23/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `himself65/2025/12/09/error-code` --- ### 📝 Commits (10+) - [`2dba0a3`](https://github.com/better-auth/better-auth/commit/2dba0a331f9760f909447f8965cbed2b917034b8) feat: return error code - [`dbc936d`](https://github.com/better-auth/better-auth/commit/dbc936de99baf55070e481671053deecfa4dbf7e) fix: error code - [`5287fc6`](https://github.com/better-auth/better-auth/commit/5287fc6315eded592aecb1aa068d3f25f24c7947) fix: type - [`601c6b0`](https://github.com/better-auth/better-auth/commit/601c6b0c6740d97ba642f96cefe238dbcac1e643) fix: type - [`6236ec6`](https://github.com/better-auth/better-auth/commit/6236ec6eac170bc37e00bc048f4c20859990520c) fix: type - [`a09587a`](https://github.com/better-auth/better-auth/commit/a09587aef0e54ef0f8be1c200b0f4ba20ebc5169) fix: code - [`2a00dac`](https://github.com/better-auth/better-auth/commit/2a00dac96420ce3342afa592e90a0595f2761969) fix: test - [`031cf1c`](https://github.com/better-auth/better-auth/commit/031cf1cca77ddf3698842bb293a9e2f444d66218) fix: test - [`e1754c4`](https://github.com/better-auth/better-auth/commit/e1754c41d911c7f5bd803e1a1e08367bf05b3a9b) test: fix - [`7ff3d98`](https://github.com/better-auth/better-auth/commit/7ff3d980df2ddddb09e3dc51512e12582c9ef07d) fix: export ### 📊 Changes **100 files changed** (+2072 additions, -1757 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/concepts/api.mdx` (+2 -2) 📝 `packages/better-auth/src/api/index.ts` (+6 -4) 📝 `packages/better-auth/src/api/middlewares/origin-check.ts` (+65 -5) 📝 `packages/better-auth/src/api/routes/account.test.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/account.ts` (+52 -52) 📝 `packages/better-auth/src/api/routes/email-verification.ts` (+30 -30) 📝 `packages/better-auth/src/api/routes/reset-password.ts` (+7 -15) 📝 `packages/better-auth/src/api/routes/session.ts` (+35 -15) 📝 `packages/better-auth/src/api/routes/sign-in.test.ts` (+3 -8) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+42 -43) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+34 -28) 📝 `packages/better-auth/src/api/routes/update-user.ts` (+45 -54) 📝 `packages/better-auth/src/api/to-auth-endpoints.test.ts` (+8 -7) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+7 -6) 📝 `packages/better-auth/src/auth/auth.test.ts` (+8 -6) 📝 `packages/better-auth/src/call.test.ts` (+8 -7) 📝 `packages/better-auth/src/client/client.test.ts` (+8 -8) 📝 `packages/better-auth/src/client/types.ts` (+7 -1) 📝 `packages/better-auth/src/db/schema.ts` (+11 -7) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+3 -2) _...and 80 more files_ </details> ### 📄 Description Fixes: https://github.com/better-auth/better-auth/issues/6938 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Standardized API error responses to always include a machine-readable code, and surfaced plugin error codes to clients for consistent handling. - **New Features** - Introduced APIError in @better-auth/core/error with from and fromStatus helpers. - Added isAPIError utility and updated docs to use it for safe error detection. - defineErrorCodes now returns { code, message }; BASE_ERROR_CODES expanded (origin/callback/redirect, email verification, sessions, etc.). - Updated all routes and plugins to return consistent { code, message } errors. - Origin/callback/redirect/errorCallbackURL/newUserCallbackURL validation now throws specific error codes. - Exposed error codes to clients via $ERROR_CODES across core plugins (admin, anonymous, api-key, email-otp, generic-oauth, multi-session, organization, phone-number, two-factor, username), plus Stripe and Passkey. - **Migration** - Client code should read error.code alongside error.message in responses. - Use isAPIError instead of instanceof checks when handling errors. - Custom plugins: update $ERROR_CODES to the new shape { code, message } and use defineErrorCodes accordingly. - When throwing errors in custom code, use APIError.from(status, errorCode) or APIError.fromStatus(status). <sup>Written for commit 7c39df9b61665169fadc373b5694892a2bbcdfd2. 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-04-15 21:55:01 -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#23696