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
- Project auto-blocked after load testing ...
Hi team π My project has been automatically blocked with the message: "Project is currently blocked β Access to this project is restricted. Contact support if...
- App build crashing with "Internal error"
Hello Appwrite team! π We are trying to deploy a Next.js application on Appwrite Cloud, but our builds are consistently failing. The deployment log successful...
- education plan not activated
Hi I have an edu id 13103046@iubat.edu but when I am trying to claim my plan and trying to logging with github where education student plan active. the appwrite...