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
- Paused project can't activate
I have failed to reactivate one my projects which had been paused
- Site deployment keeps getting failed
Hi good folks, need a hand with Sites deploy Error on every deploy: Synchronous function execution timed out... duration doesn't exceed 30 seconds [exact log ...
- Unknown attribute type: varchar / text
Since the `string` type is deprecated I tried using `varchar` and `text` in some newer tables, but when running `appwrite pull tables && appwrite types ./src/li...