[PR #8518] [CLOSED] fix(i18n): translate errors thrown from plugin before hooks #24931

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

📋 Pull Request Information

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

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


📝 Commits (1)

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

📊 Changes

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

View changed files

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

📄 Description

Summary

  • Route APIErrors from before hooks through after hooks before throwing, so after-hook-based plugins like i18n can translate them
  • Previously, before-hook errors (e.g. USERNAME_IS_ALREADY_TAKEN from the username plugin) bypassed runAfterHooks() entirely, so the i18n plugin never got a chance to translate them
  • Added optional chaining in runAfterHooks() to safely handle hooks that return undefined

Closes #8492

Test plan

  • Added test: before-hook error (USERNAME_IS_ALREADY_TAKEN) is translated when using asResponse: true
  • Added test: before-hook error is translated when calling auth.api directly (throws translated error)
  • All 17 i18n tests pass
  • All 34 to-auth-endpoints tests pass
  • All 32 username plugin tests pass
  • 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/8518 **Author:** [@himself65](https://github.com/himself65) **Created:** 3/9/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/i18n-before-hook-errors` --- ### 📝 Commits (1) - [`edcc5e0`](https://github.com/better-auth/better-auth/commit/edcc5e0da50771cc50b6fd59a9f865b4b697b12b) fix(i18n): translate errors thrown from plugin before hooks ### 📊 Changes **2 files changed** (+159 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+54 -8) 📝 `packages/i18n/src/i18n.test.ts` (+105 -0) </details> ### 📄 Description ## Summary - Route `APIError`s from before hooks through after hooks before throwing, so after-hook-based plugins like i18n can translate them - Previously, before-hook errors (e.g. `USERNAME_IS_ALREADY_TAKEN` from the username plugin) bypassed `runAfterHooks()` entirely, so the i18n plugin never got a chance to translate them - Added optional chaining in `runAfterHooks()` to safely handle hooks that return `undefined` Closes #8492 ## Test plan - [x] Added test: before-hook error (`USERNAME_IS_ALREADY_TAKEN`) is translated when using `asResponse: true` - [x] Added test: before-hook error is translated when calling `auth.api` directly (throws translated error) - [x] All 17 i18n tests pass - [x] All 34 `to-auth-endpoints` tests pass - [x] All 32 username plugin tests pass - [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-04-15 22:38:19 -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#24931