[PR #4152] [CLOSED] fix: prevent TypeError when errorWithStack is undefined in to-auth-endpoints #5224

Closed
opened 2026-03-13 12:14:47 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4152
Author: @NalinDalal
Created: 8/22/2025
Status: Closed

Base: canaryHead: canary


📝 Commits (1)

  • cc44e21 fix: prevent TypeError when errorWithStack is undefined in to-auth-endpoints

📊 Changes

1 file changed (+267 additions, -265 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.ts (+267 -265)

📄 Description

Summary

This PR fixes a bug where tests would fail with a TypeError: Cannot read properties of undefined (reading 'stack') in to-auth-endpoints.ts when errorWithStack is undefined.

Details

  • Adds a nullish check before accessing errorWithStack.stack.
  • Prevents server errors during test runs and improves error handling.

Reproduction

Run any test (e.g. pnpm test src/plugins/jwt/jwt.test.ts) on canary before this fix to see the error.


Summary by cubic

Prevents a TypeError in to-auth-endpoints by guarding stack access when errorWithStack is undefined. Stops test crashes and keeps debug stacks when available.

  • Bug Fixes
    • Use optional chaining when assigning APIError.stack from errorWithStack in endpoint and before/after hooks.
    • Avoids server/test failures and preserves stack traces in debug mode when present.

🔄 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/4152 **Author:** [@NalinDalal](https://github.com/NalinDalal) **Created:** 8/22/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `canary` --- ### 📝 Commits (1) - [`cc44e21`](https://github.com/better-auth/better-auth/commit/cc44e21da259e526948e029fbe89e30623a11f9a) fix: prevent TypeError when errorWithStack is undefined in to-auth-endpoints ### 📊 Changes **1 file changed** (+267 additions, -265 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+267 -265) </details> ### 📄 Description ## Summary This PR fixes a bug where tests would fail with a `TypeError: Cannot read properties of undefined (reading 'stack')` in `to-auth-endpoints.ts` when `errorWithStack` is undefined. ## Details - Adds a nullish check before accessing `errorWithStack.stack`. - Prevents server errors during test runs and improves error handling. ## Reproduction Run any test (e.g. `pnpm test src/plugins/jwt/jwt.test.ts`) on canary before this fix to see the error. ## Related - Traced to PR #4094 - Issue #4150 --- ## Summary by cubic Prevents a TypeError in to-auth-endpoints by guarding stack access when errorWithStack is undefined. Stops test crashes and keeps debug stacks when available. - **Bug Fixes** - Use optional chaining when assigning APIError.stack from errorWithStack in endpoint and before/after hooks. - Avoids server/test failures and preserves stack traces in debug mode when present. --- <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:14:47 -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#5224