I am calling this function to handle login, i want to handle different scenarios for password less than 8 characters, invalid credentials, etc.
const handleLogin = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
setUserLoading(true);
await account.createEmailSession(email, password);
setUserLoading(false);
router.push("/app");
} catch (error) {
console.log(error);
if (error instanceof AppwriteException) {
{/* @ts-ignore */}
alert(error.response.message)
}
}
};
So I am checking if the error is an instance of AppwriteException then I just want to alert the user the message. I can see this in the browser console e.g.
AppwriteException {
name: 'AppwriteException',
code: 401,
type: 'user_invalid_credentials',
response: {
message: 'Invalid credentials. Please check the email and password.',
code: 401,
type: 'user_invalid_credentials',
version: '0.10.28'
},
stack: 'AppwriteException: Invalid credentials. Please check the email and password.\n' +
' at Client.eval (webpack-internal:///(app-client)/./node_modules/appwrite/dist/esm/sdk.js:410:27)\n' +
' at Generator.next (<anonymous>)\n' +
' at fulfilled (webpack-internal:///(app-client)/./node_modules/appwrite/dist/esm/sdk.js:41:58)',
message: 'Invalid credentials. Please check the email and password.'
}
Just accessing error.response.message but getting this error Property 'message' does not exist on type 'string'.
web sdk version: 11.0.0
Even though it looks as an object response its actually a string.
So try something like this
} catch (error) {
console.log(error);
if (error instanceof AppwriteException) {
const response = JSON.parse(error.response ?? '{}') ?? {};
alert(response.message);
}
}
Yes it is declared as a string in client.d.ts
declare class AppwriteException extends Error {
code: number;
response: string;
type: string;
constructor(message: string, code?: number, type?: string, response?: string);
}
thinking of doing something like this
} catch (error) {
console.log(error);
if (error instanceof AppwriteException) {
const response = JSON.parse(error.response ?? '{}') ?? {};
const errorMessage = response.message || 'An unknown error occurred.';
alert(errorMessage);
}
}
It worked for you that way?
nope, it does not work
Unhandled Runtime Error
SyntaxError: "[object Object]" is not valid JSON
because i think response here is an object
if (error instanceof AppwriteException) {
{/* @ts-ignore */}
alert(error.response.message)
}
this works but i have to use @ts-ignore
Ohh, gotcha make sense - the structure, because the problem is that the type is string.
If you want to avoid the @ts-ignore you can cast it
const response = (error.response as any);
alert(response.message);
Recommended threads
- Which flutter SDK version for Self Hoste...
Hi all, Is there a good way to figure out which version of flutter SDK and Dart SDK is current for latest available self-hosted 1.8.0 ? I know new features are...
- Google Auth not working in a React App
Authentication with Google has failed. It redirects back to the signin route in React. Attached screenshots for configuration of Google console and Appwrite Go...
- Dokploy docker compose
Hey guys hope y'all doing well, I was wondering if anyone could share a working 1.8.0 docker-compose that works with Dokploy I tried making it but it just does...