From ed79156dd5946f95daf6d261e4d6b03acdb610c1 Mon Sep 17 00:00:00 2001 From: Selwyn Ang Date: Thu, 17 Oct 2024 20:37:57 +0800 Subject: [PATCH 1/3] Chaneg login failed toast message --- frontend/components/auth/login-form.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/components/auth/login-form.tsx b/frontend/components/auth/login-form.tsx index 7f415c5427..68d69b6005 100644 --- a/frontend/components/auth/login-form.tsx +++ b/frontend/components/auth/login-form.tsx @@ -45,7 +45,7 @@ export function LoginForm() { toast({ title: "Error", variant: "destructive", - description: "Login Failed.", + description: "Username or/and password provided is wrong.", }); } }; From e806e89852a234aa5dac4220503922857f712cf7 Mon Sep 17 00:00:00 2001 From: Selwyn Ang Date: Sat, 19 Oct 2024 12:55:41 +0800 Subject: [PATCH 2/3] Refine login error code handling and description --- frontend/app/auth/auth-context.tsx | 11 ++++++++++- frontend/components/auth/login-form.tsx | 19 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/frontend/app/auth/auth-context.tsx b/frontend/app/auth/auth-context.tsx index cdbd127d59..ca8a9e0be4 100644 --- a/frontend/app/auth/auth-context.tsx +++ b/frontend/app/auth/auth-context.tsx @@ -75,7 +75,16 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { ); if (!response.ok) { - throw new Error("Not OK"); + switch (response.status) { + case 400: + throw new Error("Email and/or password is missing."); + case 401: + throw new Error("Invalid email or password."); + case 500: + throw new Error("Internal server error. Please try again later."); + default: + throw new Error("Unexpected error occurred."); + } } const resJson = await response.json(); diff --git a/frontend/components/auth/login-form.tsx b/frontend/components/auth/login-form.tsx index 68d69b6005..7b238e8651 100644 --- a/frontend/components/auth/login-form.tsx +++ b/frontend/components/auth/login-form.tsx @@ -41,11 +41,26 @@ export function LoginForm() { description: "Login Failed.", }); } - } catch (err) { + } catch (err: any) { + let description_text = ""; + switch (err.message) { + case "Email and/or password is missing.": + description_text = "Please provide both email and password."; + break; + case "Invalid email or password.": + description_text = "Username or password is incorrect."; + break; + case "Internal server error. Please try again later.": + description_text = "There was an issue with the server. Please try again later."; + break; + default: + description_text = "An unexpected error occurred. Please try again."; + break; + } toast({ title: "Error", variant: "destructive", - description: "Username or/and password provided is wrong.", + description: description_text, }); } }; From d5b092fb258c0b76f90d646e9acbd6b076d18b75 Mon Sep 17 00:00:00 2001 From: Selwyn Ang Date: Sat, 19 Oct 2024 13:10:49 +0800 Subject: [PATCH 3/3] Fix linting errors --- frontend/components/auth/login-form.tsx | 50 +++++++++++++++---------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/frontend/components/auth/login-form.tsx b/frontend/components/auth/login-form.tsx index 7b238e8651..a8590a195b 100644 --- a/frontend/components/auth/login-form.tsx +++ b/frontend/components/auth/login-form.tsx @@ -41,27 +41,37 @@ export function LoginForm() { description: "Login Failed.", }); } - } catch (err: any) { - let description_text = ""; - switch (err.message) { - case "Email and/or password is missing.": - description_text = "Please provide both email and password."; - break; - case "Invalid email or password.": - description_text = "Username or password is incorrect."; - break; - case "Internal server error. Please try again later.": - description_text = "There was an issue with the server. Please try again later."; - break; - default: - description_text = "An unexpected error occurred. Please try again."; - break; + } catch (err: unknown) { + if (err instanceof Error) { + let description_text = ""; + switch (err.message) { + case "Email and/or password is missing.": + description_text = "Please provide both email and password."; + break; + case "Invalid email or password.": + description_text = "Username or password is incorrect."; + break; + case "Internal server error. Please try again later.": + description_text = + "There was an issue with the server. Please try again later."; + break; + default: + description_text = + "An unexpected error occurred. Please try again."; + break; + } + toast({ + title: "Error", + variant: "destructive", + description: description_text, + }); + } else { + toast({ + title: "Error", + variant: "destructive", + description: "An unexpected error occurred. Please try again.", + }); } - toast({ - title: "Error", - variant: "destructive", - description: description_text, - }); } };