Skip to content

fix(login): show error toast on sgID profile fetch non-401 errors#9543

Open
Strikerprv wants to merge 1 commit into
opengovsg:developfrom
Strikerprv:fix/sgid-profile-fetch-error-handling
Open

fix(login): show error toast on sgID profile fetch non-401 errors#9543
Strikerprv wants to merge 1 commit into
opengovsg:developfrom
Strikerprv:fix/sgid-profile-fetch-error-handling

Conversation

@Strikerprv
Copy link
Copy Markdown

Problem

SelectProfilePage unconditionally redirects all errors from useSgidProfiles to the login page with no user feedback. Only a 401 (unauthenticated) is a valid reason to redirect silently — all other errors (5xx, other 4xx, network failures) left users stuck in a silent redirect loop with no indication that the service was at fault.

Closes #9540

Solution

Gate the redirect on the error status code. Previously all errors redirected silently to /login with no feedback. Now all errors redirect to /login?status=<code>, leveraging the existing LoginPage status-based toast mechanism to surface a user-facing message appropriate to the error type:

  • 401 → "Your session has expired, please log in again"
  • 403 → "You are not authorised to access this page"
  • 5xx → "A server error occurred. Please try again later."
  • other → generic error message: "Something went wrong. Please try again later."

5xx range handling and the errors.serverError translation key were added to support the new explicit server error message.

Breaking Changes

  • Yes - this PR contains breaking changes
  • No - this PR is backwards compatible

Bug Fixes:

  • SelectProfilePage: all errors now redirect to /login?status=<code> instead of silently redirecting to /login with no feedback, allowing LoginPage to surface the appropriate error toast per status code
  • LoginPage: added 5xx range handling in the status-based toast switch — codes 500–599 now show 'A server error occurred. Please try again later.' instead of the generic fallback
  • i18n: added errors.serverError translation key to features/common to support the explicit server error message

Before & After Screenshots

BEFORE:
User hits a 5xx on the sgID profiles endpoint → silently redirected to /login → re-attempts → infinite loop, no error shown

AFTER:
User hits a 500 → redirected to /login?status=500 → toast appears: 'A server error occurred. Please try again later.'

image

Tests

  1. Override useSgidProfiles in queries.ts to throw Object.assign(new Error('Simulated 503'), { code: 503 })
  2. Go through the sgID login flow
  3. Confirm redirect lands on /login?status=503 with a server error toast visible
  4. Change to { code: 401 } — confirm redirect lands on /login?status=401 with an expired session toast
  5. Change to a plain new Error('network error') (no .code) — confirm redirect lands on /login?status=500 with a generic error toast
  6. Revert queries.ts and confirm normal sgID login flow is unaffected

@Strikerprv Strikerprv requested a review from a team as a code owner June 3, 2026 15:25
@LoneRifle LoneRifle requested a review from Copilot June 4, 2026 08:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the sgID login UX by ensuring sgID profile-fetch failures surface an error toast on the login page (instead of silently redirecting and potentially looping), and adds explicit messaging for 5xx server errors.

Changes:

  • Update SelectProfilePage to redirect to /login?status=<code> for non-401 sgID profile fetch errors.
  • Extend LoginPage’s status-to-toast mapping to treat 500–599 as a dedicated “server error” message.
  • Add errors.serverError to the common i18n typings and en-sg locale.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
apps/frontend/src/i18n/locales/features/common/index.ts Adds serverError key to the Common i18n type.
apps/frontend/src/i18n/locales/features/common/en-sg.ts Adds the serverError message string in en-sg.
apps/frontend/src/features/login/SelectProfilePage.tsx Redirects sgID profile-fetch errors to login with an attached status code (except 401).
apps/frontend/src/features/login/LoginPage.tsx Adds 5xx-range handling to show a dedicated server-error toast.

Comment on lines +136 to +151
if (profilesResponse.error) {
if (
(profilesResponse.error as HttpError).code === StatusCodes.UNAUTHORIZED
) {
// 401 — session invalid, redirect quietly
window.location.replace(LOGIN_ROUTE)
} else {
// all other errors (5xx, network failure, etc.) — redirect with status
// so LoginPage can surface the generic error toast on arrival.
// Fall back to 500 for network failures where no status code is present.
const statusCode =
(profilesResponse.error as HttpError).code ??
StatusCodes.INTERNAL_SERVER_ERROR
window.location.replace(`${LOGIN_ROUTE}?status=${statusCode}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] SelectProfilePage silently redirects to login on any sgID profile fetch error, causing an infinite loop

3 participants