How are you supposed to handle errors on the client? API is very inconsistent

- I appreciate all the efforts the authors have put into making this library but I'll be brutally honest - Something as basic as error handling has absolutely horrible documentation - I am talking specifcally about handling errors in my sveltekit client that uses a separate express backend - Now before you point it, yes I have read the docs
catch (error) {
if (error instanceof BetterAuthError) {
formError = error.message || 'An error occurred. Please try again.';
console.error('Forgot password request failed:', JSON.stringify(error));
}
else if (error instanceof BetterFetchError) {
formError = error.error.message || error.message || 'An error occurred. Please try again.';
console.error('Forgot password request failed:', JSON.stringify(error));
} else {
formError = 'An unexpected error occurred. Please try again.';
console.error('Unexpected error during forgot password request:', error);
}
}
catch (error) {
if (error instanceof BetterAuthError) {
formError = error.message || 'An error occurred. Please try again.';
console.error('Forgot password request failed:', JSON.stringify(error));
}
else if (error instanceof BetterFetchError) {
formError = error.error.message || error.message || 'An error occurred. Please try again.';
console.error('Forgot password request failed:', JSON.stringify(error));
} else {
formError = 'An unexpected error occurred. Please try again.';
console.error('Unexpected error during forgot password request:', error);
}
}
- This is my current catch block for all the forms like login, signup, forgot and reset - Nothing fancy, just email and password with a cloudflare turnstile setup on the server - Let me tell you the issues in this - Sometimes the BetterAuthError object will have the following structure
{"status":400,"statusText":"Bad Request","error":{"message":"Missing CAPTCHA response"}}
{"status":400,"statusText":"Bad Request","error":{"message":"Missing CAPTCHA response"}}
Client | Better Auth
Better Auth client library for authentication.
1 Reply
tacit_danger
tacit_dangerOP4w ago
- Notice how there is no code here. I triggered this by simply not giving the backend a CAPTCHA token - Shut the backend down comnpletely and it triggers the else block where the network error gets handled - Now lets give it an invalid email and see what happens after disabling all the HTML validation measures
{"status":400,"statusText":"Bad Request","error":{"code":"VALIDATION_ERROR","message":"Invalid body parameters"}}
{"status":400,"statusText":"Bad Request","error":{"code":"VALIDATION_ERROR","message":"Invalid body parameters"}}
- Funnily it says nothing that the email is the field that is invalid - So when is BetterAuthError thrown on the frontend? Do I need to catch this? - How do I differentiate between the conditions where error object only contains a message vs error containing both a code and a message? How do I know which field failed the validation error? What about CORS errors? - Guys, this really needs better documentation and work

Did you find this page helpful?