
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
- Error getting session: AppwriteException...
I get this error `Error getting session: AppwriteException: User (role: guests) missing scope (account)` when running in prod. As soon as I try running my app o...
- PR Review and Issue Assign?
I am not familiar with how things work here. I know that Issue have to be assigned before solving problem, It is for not wasting contributors time but I like t...
- 500 internal error when trying to access...
Hello team, I am having trouble trying to access my projects in Appwrite. Each time I try to click in a project I want to open it doesn't load immediately and ...
