Back

Type Definition of AppwriteException

  • 1
  • General
  • Web
Ali Memon
4 Jun, 2023, 18:55

I am calling this function to handle login, i want to handle different scenarios for password less than 8 characters, invalid credentials, etc.

TypeScript
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.

TypeScript
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

TL;DR
The user is trying to handle different scenarios for login errors in an app. They are using the AppwriteException class, but are having trouble accessing the 'message' property of the error.response object. The issue is that error.response is actually a string, not an object. The user tries different approaches, including casting and parsing the response, but still encounters errors. The solution provided is to parse the response as JSON and then access the message property. The user implements this solution in their handleLogin function.
Binyamin
4 Jun, 2023, 18:59

Even though it looks as an object response its actually a string. So try something like this

TypeScript
 } catch (error) {
      console.log(error);
      if (error instanceof AppwriteException) {
        const response = JSON.parse(error.response ?? '{}') ?? {};
        alert(response.message);
      }
    }
Ali Memon
5 Jun, 2023, 08:32

Yes it is declared as a string in client.d.ts

TypeScript
declare class AppwriteException extends Error {
    code: number;
    response: string;
    type: string;
    constructor(message: string, code?: number, type?: string, response?: string);
}
Ali Memon
5 Jun, 2023, 08:40

thinking of doing something like this

TypeScript
} 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);
}
    }
Binyamin
5 Jun, 2023, 12:03

It worked for you that way?

Ali Memon
6 Jun, 2023, 11:52

nope, it does not work

TypeScript
Unhandled Runtime Error
SyntaxError: "[object Object]" is not valid JSON

because i think response here is an object

TypeScript
if (error instanceof AppwriteException) {
        {/* @ts-ignore */}
        alert(error.response.message)
      }

this works but i have to use @ts-ignore

Binyamin
6 Jun, 2023, 13:18

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

TypeScript
  const response = (error.response as any);

  alert(response.message);
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more