Back

[Solved] Make SDK functions fail silently?

  • 0
  • Web
saricden
14 Jun, 2023, 13:45

Is it possible to make the SDK functions fail without logging about it in the JS console?

I'm wrapping the call in question in a try { ... } catch (e) { ... } statement, however a 404 is still being reported.

My use case is I'm using a team to signify admin users, and checking each user to see if they belong to the admin team to determine whether or not to show the admin UI.

I'm currently teams.listMemberships to query if the current user is a member of the 'admin' team. If there is a better way of doing this let me know! πŸ™‚

TL;DR
The user wants to find a way to silence the 404 errors being reported in the console when using the SDK function `teams.listMemberships()`. One suggestion is to use `teams.list()` instead and check if the admin team is returned. Another solution proposed is to wrap the code inside a function and use a `try/catch` block to catch the error silently. The suggested code for this solution is provided in the thread. There is no confirmation if either solution worked for the user.
Binyamin
14 Jun, 2023, 13:46

What you mean by 404 from who? on the REST request? Also, is this is the cloud or a self-hosted?

saricden
14 Jun, 2023, 13:53

This will be a cloud project, and the 404 in question is coming from the following code (on a non-admin user):

TypeScript
let isAdmin = false;

try {
  const adminResults = await teams.listMemberships('admin', undefined, auth_id!);

  if (adminResults.memberships.length === 1) {
    const {userId: adminUserId} = adminResults.memberships[0];

    isAdmin = (auth_id === adminUserId);
  }
}
catch (e) {}
saricden
14 Jun, 2023, 13:53

Perhaps this is just Chrome's default behaviour?

saricden
14 Jun, 2023, 13:54

It's not the end of the world, it's just I'm expecting it to 404 in a lot (the majority) of cases, so if I can suppress this somehow it'd make for a much cleaner console

Binyamin
14 Jun, 2023, 13:56

Ohh got you. I thought you meant Appwrite functions

Binyamin
14 Jun, 2023, 13:56

So, In this case

saricden
14 Jun, 2023, 13:56

Oh no, client SDKs, my apologies

Binyamin
14 Jun, 2023, 13:56

No no, my bad

Binyamin
14 Jun, 2023, 13:58

Appwrite uses throw logic to handle this things. So to make cleaner or silently failed it will probably be best to wrap it within a function, like so:

TypeScript
function isAdmin() {
  try {
    const adminResults = await teams.listMemberships('admin', undefined, auth_id!);

    if (adminResults.memberships.length === 1) {
      const {userId: adminUserId} = adminResults.memberships[0];

      return (auth_id === adminUserId);
    }
  }
  catch (e) {}

  return false;
}
saricden
14 Jun, 2023, 14:03

What makes this try / catch different from what I have? I'm still awaiting the results of the listMemberships() promise inside a try statement, so I'm a little confused here.

It might also be worth noting that my original code is already nested inside another try/catch (which includes reaching out for prior user data), and is already inside a reusable function getUserData(auth_id), the goal of which is to retrieve user data about a given user (including if they have admin privileges or not).

saricden
14 Jun, 2023, 14:04

I do get the feeling this may just be Chrome reporting a network request has failed tho

saricden
14 Jun, 2023, 14:04

In which case there's likely not much I can do

Binyamin
14 Jun, 2023, 14:20

I doesn't make different logically, just programmatic flow.

Binyamin
14 Jun, 2023, 14:20

When team not found, you'll always going to get 404.

Binyamin
14 Jun, 2023, 14:21

But when the error is enclosed within you try/catch block, then, it doesn't affect your app and the end user

Binyamin
14 Jun, 2023, 14:21

Is that make sense?

saricden
14 Jun, 2023, 14:26

Yeah that makes sense and is how I was using it, but what I'm looking for is a way to silence the 404 being reported in the console, so that if a dev-user pops it open they don't see 10000 404 errors

Binyamin
14 Jun, 2023, 14:27

Mmm, I don't think you can do that. Is like you can't hide any other XHR requests to your Appwrite.

saricden
14 Jun, 2023, 14:27

Yeahh, I kinda figured, oh well thought I'd ask

saricden
14 Jun, 2023, 14:27

Thank you πŸ™‚

joeyouss
14 Jun, 2023, 14:46

Please mark this post as SOLVED or CLOSED if no further help is needed.

saricden
14 Jun, 2023, 14:57

[Solved] Make SDK functions fail silently?

Drake
14 Jun, 2023, 18:25

i wouldn't call teams.listMemberships(). I would use teams.list() and check if the admin team is returned

saricden
14 Jun, 2023, 18:29

Okay thanks Steven, I'll revisit this in a bit πŸ‘

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