Skip to content

Commit e806e89

Browse files
committed
Refine login error code handling and description
1 parent ed79156 commit e806e89

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

frontend/app/auth/auth-context.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
7575
);
7676

7777
if (!response.ok) {
78-
throw new Error("Not OK");
78+
switch (response.status) {
79+
case 400:
80+
throw new Error("Email and/or password is missing.");
81+
case 401:
82+
throw new Error("Invalid email or password.");
83+
case 500:
84+
throw new Error("Internal server error. Please try again later.");
85+
default:
86+
throw new Error("Unexpected error occurred.");
87+
}
7988
}
8089

8190
const resJson = await response.json();

frontend/components/auth/login-form.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ export function LoginForm() {
4141
description: "Login Failed.",
4242
});
4343
}
44-
} catch (err) {
44+
} catch (err: any) {
45+
let description_text = "";
46+
switch (err.message) {
47+
case "Email and/or password is missing.":
48+
description_text = "Please provide both email and password.";
49+
break;
50+
case "Invalid email or password.":
51+
description_text = "Username or password is incorrect.";
52+
break;
53+
case "Internal server error. Please try again later.":
54+
description_text = "There was an issue with the server. Please try again later.";
55+
break;
56+
default:
57+
description_text = "An unexpected error occurred. Please try again.";
58+
break;
59+
}
4560
toast({
4661
title: "Error",
4762
variant: "destructive",
48-
description: "Username or/and password provided is wrong.",
63+
description: description_text,
4964
});
5065
}
5166
};

0 commit comments

Comments
 (0)