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
- CORS errors in Obsidian custom plugin
Hi, anyone here familiar with obsidian community plugins? In short: it's a local first note app which supports writing your own add-ons / plugin But I keep get...
- > AppwriteException: The requested servi...
When trying to read or write from my database I get the following error: > AppwriteException: The requested service is disabled. You can enable the service from...
- Courtesy limit reset for non-profit migr...
Hi Team! I'm the architect for a 501(c)(3) non-profit project (Aaria's Blue Elephant) and we just hit our Free plan Database Read limit (currently at 164%). Th...