[GH-ISSUE #2582] APIError not exported (foils instanceof checks) issue when bundling (+fix/workaround) #9262

Closed
opened 2026-04-13 04:41:18 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @firxworx on GitHub (May 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2582

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

reproduced in app with vite bundler + pnpm

  1. try import { APIError } from 'better-auth' then perform an instanceof check with it to see if a given error is an instanceof APIError
  2. note that the import is fine and types are fine but build/bundle will fail: "APIError" is not exported by ... better-auth

Current vs. Expected behavior

Current

The following code appears to work because better-auth exports the type:

import { APIError } from 'better-auth'

// ... 

const isBetterAuthError = someError instanceof APIError

However it will fail to build/bundle because the actual APIError is not there.

Expected

I expect to be able to import the main error thrown by a key package like better-auth and use it in instanceof checks without build failures.

Analysis

I checked the better-auth code and APIError is from better-call package and the allowed version range is defined in catalog in pnpm-workspace.json

Adding better-call as a dependency alongside better-auth helped... pnpm likes that!

A greater workaround in a pnpm workspace with an auth helper package was to create an isBetterAuthError() type guard that checks if the input is instanceof APIError where APIError is imported from better-call and as a fallback will check the shape of the input and confirm it has the expected properties. The idea here is to confidently detect it for conditional logging + user feedback purposes.

Suggestion

Please consider the suggestion that Better Auth might benefit from its own custom error class (or classes that extend from a common base class).

It would then use its own error(s) throughout and export as part of the better-auth package.

Absolutely you can still use better-call if that's what the maintainer team loves but please extend it.

This way errors from Better Auth can easily be distinguished in conditional error handling code and any build/bundle issues would not be a thing.

What version of Better Auth are you using?

1.2.7

Provide environment information

- OS/browser irrelevant
- pnpm + vite + better-auth all current versions

Which area(s) are affected? (Select all that apply)

Package

Auth config (if applicable)

N/A

Additional context

I'm running latest current release of everything - pnpm, vite, better-auth, etc.

Originally created by @firxworx on GitHub (May 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2582 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce reproduced in app with vite bundler + pnpm 1. try `import { APIError } from 'better-auth'` then perform an `instanceof` check with it to see if a given error is an instanceof `APIError` 2. note that the import is fine and types are fine but build/bundle will fail: `"APIError" is not exported by` ... `better-auth` ### Current vs. Expected behavior ## Current The following code appears to work because better-auth exports the type: ```ts import { APIError } from 'better-auth' // ... const isBetterAuthError = someError instanceof APIError ``` However it will fail to build/bundle because the actual APIError is not there. ## Expected I expect to be able to import the main error thrown by a key package like `better-auth` and use it in `instanceof` checks without build failures. ## Analysis I checked the better-auth code and `APIError` is from `better-call` package and the allowed version range is defined in catalog in `pnpm-workspace.json` Adding `better-call` as a dependency alongside `better-auth` helped... pnpm likes that! A greater workaround in a pnpm workspace with an auth helper package was to create an `isBetterAuthError()` type guard that checks if the input is `instanceof APIError` where `APIError` is imported from `better-call` and as a fallback will check the shape of the input and confirm it has the expected properties. The idea here is to confidently detect it for conditional logging + user feedback purposes. ## Suggestion Please consider the suggestion that Better Auth might benefit from its own custom error class (or classes that extend from a common base class). It would then use its own error(s) throughout and export as part of the `better-auth` package. Absolutely you can still use `better-call` if that's what the maintainer team loves but please _extend_ it. This way errors from Better Auth can easily be distinguished in conditional error handling code and any build/bundle issues would not be a thing. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - OS/browser irrelevant - pnpm + vite + better-auth all current versions ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript N/A ``` ### Additional context I'm running latest current release of everything - pnpm, vite, better-auth, etc.
GiteaMirror added the locked label 2026-04-13 04:41:18 -05:00
Author
Owner

@pauksztello commented on GitHub (May 8, 2025):

If you are using API:

import { APIError } from "better-auth/api";

<!-- gh-comment-id:2864249566 --> @pauksztello commented on GitHub (May 8, 2025): If you are using API: `import { APIError } from "better-auth/api";`
Author
Owner

@Kinfe123 commented on GitHub (May 13, 2025):

yeah we do export APIError from better-auth/api.

<!-- gh-comment-id:2877889979 --> @Kinfe123 commented on GitHub (May 13, 2025): yeah we do export APIError from `better-auth/api`.
Author
Owner

@firxworx commented on GitHub (May 24, 2025):

OK thanks guys I still would counter that having an export of APIError from better-auth is an issue, as is the fact I couldn't find this in the docs (not saying its not there, I just couldn't find it).

People and AI's alike will look at the exports and try it, autocomplete will suggest it, and it won't work.

This presents an issue because in code and type check everything looks fine, it only fails during actual bundling/build.

Anyway I appreciate it and your hard work on this project! Overall better-auth is an incredible addition to the ecosystem :)

<!-- gh-comment-id:2906057127 --> @firxworx commented on GitHub (May 24, 2025): OK thanks guys I still would counter that having an export of `APIError` from `better-auth` is an issue, as is the fact I couldn't find this in the docs (not saying its not there, I just couldn't find it). People and AI's alike will look at the exports and try it, autocomplete will suggest it, and it won't work. This presents an issue because in code and type check everything looks fine, it only fails during actual bundling/build. Anyway I appreciate it and your hard work on this project! Overall better-auth is an incredible addition to the ecosystem :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9262