fix(web): improve network error message with i18n

- Replace hardcoded 'Network error' with i18n key 'apiError.networkError'
- Add dedicated handler for status 0 in handleApiError
- Add test case for network error scenario

Closes #160
This commit is contained in:
dongmucat 2026-03-27 17:27:24 +08:00 • committed by Xudong Sun
parent 3ac66fb6d9
commit 1aae926944
3 changed files with 16 additions and 1 deletions

View file

@ -213,7 +213,7 @@ export async function fetchJson<T>(input: RequestInfo | URL, init?: RequestWithT
if (error instanceof DOMException && error.name === 'AbortError') {
throw new ApiError('error.request.timeout', 408)
}
throw new ApiError('Network error', 0)
throw new ApiError('apiError.networkError', 0)
} finally {
cleanup()
}

View file

@ -53,4 +53,14 @@ describe('handleApiError', () => {
expect(errorSpy).toHaveBeenLastCalledWith('Server said no')
})
it('shows network error message for status 0', async () => {
const { ApiError, handleApiError } = await import('./api-error')
handleApiError(new ApiError('apiError.networkError', 0))
expect(errorSpy).toHaveBeenCalled()
const lastCall = errorSpy.mock.calls[errorSpy.mock.calls.length - 1][0]
expect(lastCall).toMatch(/network|网络/)
})
})

View file

@ -52,6 +52,11 @@ export function handleApiError(error: unknown): void {
const { status } = error
if (status === 0) {
toast.error(i18n.t('apiError.networkError'))
return
}
if (status === 401) {
if (isAccountDisabledError(error)) {
window.location.href = `/login?reason=${ACCOUNT_DISABLED_REASON}`