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
- Realtime api and labels as permission
in my tables i set labels as permission and real-time capabilities stopped working. Before when i was having "any" role everything was working. Note: user have...
- "Restore project" button fails: "Invalid...
In the dashboard, it clicking "Restore project" fails. The request sent to `PATCH https://cloud.appwrite.io/v1/projects/:project_id` with payload `{status: "act...
- how to access the value of account statu...