As the title says. I want to log my users in but sometimes they get an error. For instance password too short or maybe credentials dont match.
Is there a way i can catch the error type and turn that into a variable i can use.
const IsLoggedIn = async () => {
try {
var res = await account.get();
if (res) {
redirectTo("./homepage");
}
} catch (e) {
setLoading(false);
}
};
async function handleLogin() {
try {
setLoading(true);
await account.createEmailSession(email, password);
setPassword("");
setLoading(false);
IsLoggedIn();
} catch (error) {
console.error(error);
setLoading(false);
}
}
async function handleRegister() {
try {
setLoading(true);
await account.create(ID.unique(), email, password);
setPassword("");
setLoading(false);
IsLoggedIn();
} catch (error) {
console.error(error);
setLoading(false);
}
}
const redirectTo = (path) => {
router.replace(path);
};
useEffect(() => {
IsLoggedIn();
}, []);
A bit off of what you've asked but instead of doing setLoading(false)
in both the try and catch I believe you can utilize finally and put it there.
If you want the specific error message you can do error as Error
and then error.message
to get the specific error message, and use that to display to the user.
ill try it out.
setLoading was first so i could have a spinner.
Sure, you can do setLoading(true) outside the try catch and add a finally block to the trycatch to set loading to false.
Maybe something like
setLoading(true)
try {
...something
} catch (error) {
if(error instanceof Error) {
setError(error.message)
}
} finally {
setLoading(false)
}
On AppwriteException catch (e) { }
Recommended threads
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...
- Deploy function not working - 503
Hellon i get this error message, when i try to deploy a new version of a function <html><body><h1>503 Service Unavailable</h1>No server is available to handle...
- Error When load the website
Hi, I am getting this error whenever I reload my website please help me, I am using react Error: ** GET https://cloud.appwrite.io/v1/account 401 (Unauthoriz...