/* the code that is doing the API call, account.get() is an endpoint provided by Appwrite to get current user session data */
export async function getCurrentUser() {
try {
const user = await account.get(); //api call through appwrite endpoint
return user;
}
catch (err) {
console.log("no user found : ", err);
return null;
}
}
/* here I am calling the above async function on a button click */
async function handleClick() {
try {
const res = await getCurrentUser();
console.log(res);
}
catch (err) {
console.log("error : ", err);
}
}
I am using appwrite for my backend. The api request returns a promise as response.
While executing the above code, I am getting an error in the console. I have used try/catch and async/await. I cannot understand why the error is showing up in the console.
I am getting an unauthorized error. My credentials are correct, I have checked. The error happens only if I try to get a current user session before creating a user session. If I have a user session then the error is gone.
I am basically trying to check if a user session exits or not using useEffect() and update a state variable based on that. I am asking why the error is showing up in the console if I have written a "catch" block for it and used "await" also. The catch block message should only be shown in the console according to me.
2
Answers
try this ,
just for unhandled promise rejections.
I dont think it is possible to catch the network error, as it looks like its written out to the console by code inside the appWrite library.
Ive tried attaching a catch directly to the promise
const user = await account.get().catch(console.log("err"));
But this doesnt work either.
It looks like in AppWrites own documentation that they simply use a
catch
and allow the network error to be shown in the console.