[PR #8517] fix(i18n): translate errors thrown from plugin before hooks #8030

Open
opened 2026-03-13 13:57:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8517
Author: @himself65
Created: 3/9/2026
Status: 🔄 Open

Base: canaryHead: fix/i18n-before-hook-translation


📝 Commits (1)

  • c37fc40 fix(i18n): translate errors thrown from plugin before hooks

📊 Changes

2 files changed (+96 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.ts (+42 -1)
📝 packages/i18n/src/i18n.test.ts (+54 -0)

📄 Description

Summary

  • When a plugin's before hook throws an APIError (e.g. USERNAME_IS_ALREADY_TAKEN from the username plugin), the error previously propagated immediately without running after hooks. This meant the i18n plugin's after hook never got a chance to translate the error message.
  • Now, APIErrors from before hooks are caught, stored in context.returned, and routed through runAfterHooks before being re-thrown — matching the same flow used for endpoint handler errors.

Closes #8492

Test plan

  • Added test: i18n translates USERNAME_IS_ALREADY_TAKEN error thrown from username plugin's before hook
  • All existing i18n tests pass (16/16)
  • All existing username plugin tests pass (32/32)
  • All existing sign-up tests pass (24/24)
  • pnpm typecheck passes
  • pnpm lint passes

🔄 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/8517 **Author:** [@himself65](https://github.com/himself65) **Created:** 3/9/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `fix/i18n-before-hook-translation` --- ### 📝 Commits (1) - [`c37fc40`](https://github.com/better-auth/better-auth/commit/c37fc407fbf42d4553df9b4e9382017f74ce3512) fix(i18n): translate errors thrown from plugin before hooks ### 📊 Changes **2 files changed** (+96 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+42 -1) 📝 `packages/i18n/src/i18n.test.ts` (+54 -0) </details> ### 📄 Description ## Summary - When a plugin's before hook throws an `APIError` (e.g. `USERNAME_IS_ALREADY_TAKEN` from the username plugin), the error previously propagated immediately without running after hooks. This meant the i18n plugin's after hook never got a chance to translate the error message. - Now, `APIError`s from before hooks are caught, stored in `context.returned`, and routed through `runAfterHooks` before being re-thrown — matching the same flow used for endpoint handler errors. Closes #8492 ## Test plan - [x] Added test: i18n translates `USERNAME_IS_ALREADY_TAKEN` error thrown from username plugin's before hook - [x] All existing i18n tests pass (16/16) - [x] All existing username plugin tests pass (32/32) - [x] All existing sign-up tests pass (24/24) - [x] `pnpm typecheck` passes - [x] `pnpm lint` passes --- <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 13:57:22 -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#8030