Back

How to handle exception when calling account.get()

  • 0
  • Users
  • Accounts
  • General
  • Web
kathelia.dokgu
19 Jul, 2023, 02:17

On page load I check if the user is logged in and then store that account information to a global state. The documentation says the account.get() call throws an AppwriteException so I wrap it in a try catch block but it still shows on the dev tools:

TypeScript
try {
  const userSession = await account.get();

  if (userSession) {
    setSession(userSession);
  }
} catch (error: any) {
  // AppwriteException: User (role: guests) missing scope (account)
  // User is not logged in - do nothing
}

Even though I wrapped this in a try catch block, I still get GET https://appwrite.mydomain.com/v1/account 401 on the dev tools. I know it is harmless but is it possible to not have that show up on the dev tools? Would it be possible to set these async calls so that you can destructure the data returned or the error if an error does occur:

TypeScript
const { data: userSession, error } = await account.get();
TL;DR
The user wants to handle exceptions when calling `account.get()` in JavaScript and is currently using a try-catch block. They are concerned about the error message appearing in the console and are wondering if there is a way to handle the error more cleanly. The user also suggests using destructuring to separate the data and error in the async call. The recommended solution is to continue using the try-catch block as it is the correct way to handle exceptions. The error appearing in the console is expected behavior for HTTP calls with status codes above 400. The current implementation is correct. Destructuring the data and error in the async call can be
Drake
19 Jul, 2023, 02:19

Yep, that's expected. You're doing it right. Those show up whenever there's any http call that results in > 400 status code

kathelia.dokgu
19 Jul, 2023, 02:20

Oh I see.. hmmm.. I guess I can just leave it then. I just hate how it feels like I coded things wrong whenever I see errors like that on the console.

kathelia.dokgu
19 Jul, 2023, 02:35

Isn't that what I'm already doing?

Binyamin
19 Jul, 2023, 02:36

Yes, my bad

Olivier Pavie
19 Jul, 2023, 05:59

I feel that this is the right way of doing things... my only complain is with the "any" for the error type.... i just feel that it should be strongly typed... something like catch (error: AppwriteException) because the sdk always throws this...

kathelia.dokgu
19 Jul, 2023, 17:01

@Olivier Pavie no I think that's fine, in Javascript you can throw any errors as mentioned here https://fettblog.eu/typescript-typing-catch-clauses/ so it needs to be any, you can then use instanceof to check its type:

TypeScript
try {
  const userSession = await account.get();

  if (userSession) {
    setSession(userSession);
  }
} catch (error: any) {
  if (error instanceof AppwriteException) {
    if (error.message.toLowerCase() === 'user (role: guests) missing scope (account)') {
      // AppwriteException: User (role: guests) missing scope (account)
      // User is not logged in - do nothing
    }
  }
}
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