createAuthClient().signIn.email({...}) does not call onError() in case of a fetch failure #280

Closed
opened 2026-03-13 07:40:36 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @bfovez on GitHub (Nov 25, 2024).

Describe the bug
createAuthClient().signIn.email({...},{onError:...}) does not call onError() in case of a fetch failure.

To Reproduce
Steps to reproduce the behavior:

  1. Create a client Auth on an API that makes the fetch fail on purpose:
// http://localhost:5174 makes the fetch fail on purpose
const auth = createAuthClient({ baseURL: 'http://localhost:5174' }) 
  1. Call signIn:
await auth.signIn.email(
	{ email, password },
	{
		onError: (ctx) => {
			console.error('signIn error ' + ctx.error.message)
		}
	}
)
  1. A "Failed to fetch" error is thrown, and onError is never called. We have to try / catch the whole thing and call the same code that lies within onError manually.

Expected behavior
auth.signIn.email an other auth methods should catch any exception and call the onError callback accordingly, for better DX.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

OS: Windows 11
Browser: Chrome
Version: Node 21

Originally created by @bfovez on GitHub (Nov 25, 2024). **Describe the bug** `createAuthClient().signIn.email({...},{onError:...})` does not call `onError()` in case of a fetch failure. **To Reproduce** Steps to reproduce the behavior: 1. Create a client Auth on an API that makes the fetch fail on purpose: ```ts // http://localhost:5174 makes the fetch fail on purpose const auth = createAuthClient({ baseURL: 'http://localhost:5174' }) ``` 2. Call signIn: ```ts await auth.signIn.email( { email, password }, { onError: (ctx) => { console.error('signIn error ' + ctx.error.message) } } ) ``` 3. A "Failed to fetch" error is thrown, and `onError` is never called. We have to try / catch the whole thing and call the same code that lies within `onError` manually. **Expected behavior** `auth.signIn.email` an other auth methods should catch any exception and call the `onError` callback accordingly, for better DX. **Screenshots** If applicable, add screenshots to help explain your problem. **Desktop (please complete the following information):** OS: Windows 11 Browser: Chrome Version: Node 21
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

This is expected behavior. The client only catches API returned errors not unexpected ones like fetch failed.

@Bekacru commented on GitHub (Nov 26, 2024): This is expected behavior. The client only catches API returned errors not unexpected ones like `fetch failed`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#280