Skip to content

Commit

Permalink
[Login] Improve error specificity (#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Sep 27, 2024
1 parent 2184716 commit f08ded3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"forgotPassword": "Forgot password?",
"login": "Log in",
"failed": "Failed to log in. Please check your username and password.",
"failedUnknownReason": "Failed to log in. Something went wrong with The Combine.",
"backToLogin": "Back to login",
"signUp": "Sign Up",
"signUpNew": "Sign Up New User",
Expand Down
19 changes: 8 additions & 11 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ const config = new Api.Configuration(config_parameters);
/** A list of URL patterns for which the frontend explicitly handles errors
* and the blanket error pop ups should be suppressed.*/
const whiteListedErrorUrls = [
"users/authenticate",
"users/captcha",
"/speakers/create/",
"/speakers/update/",
"/users/authenticate",
"/users/captcha/",
];

// Create an axios instance to allow for attaching interceptors to it.
Expand All @@ -66,16 +66,13 @@ axiosInstance.interceptors.response.use(undefined, (err: AxiosError) => {
router.navigate(Path.Login);
}

// Check for fatal errors (4xx-5xx).
if (
status >= StatusCodes.BAD_REQUEST &&
status <= StatusCodes.NETWORK_AUTHENTICATION_REQUIRED
) {
// Suppress error pop-ups for URLs the frontend already explicitly handles.
if (url && whiteListedErrorUrls.some((u) => url.includes(u))) {
return Promise.reject(err);
}
// Suppress error pop-ups for URLs the frontend already explicitly handles.
if (url && whiteListedErrorUrls.some((u) => url.includes(u))) {
return Promise.reject(err);
}

// Check for fatal errors (400+).
if (status >= StatusCodes.BAD_REQUEST) {
console.error(err);
enqueueSnackbar(`${status} ${response.statusText}\n${err.config.url}`);
}
Expand Down
9 changes: 8 additions & 1 deletion src/components/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export enum LoginId {
export default function Login(): ReactElement {
const dispatch = useAppDispatch();

const loginError = useAppSelector(
(state: StoreState) => state.loginState.error
);
const status = useAppSelector(
(state: StoreState) => state.loginState.loginStatus
);
Expand Down Expand Up @@ -147,7 +150,11 @@ export default function Login(): ReactElement {
style={{ color: "red", marginBottom: 24, marginTop: 24 }}
variant="body2"
>
{t("login.failed")}
{t(
loginError.includes("401")
? "login.failed"
: "login.failedUnknownReason"
)}
</Typography>
)}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/Redux/LoginActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function asyncLogIn(username: string, password: string) {
router.navigate(Path.ProjScreen);
})
.catch((err) =>
dispatch(loginFailure(err.response?.data ?? err.message))
dispatch(loginFailure(`${err.response?.status ?? err.message}`))
);
};
}
Expand Down

0 comments on commit f08ded3

Please sign in to comment.