mirror of
https://github.com/iflytek/skillhub.git
synced 2026-10-10 03:27:54 +00:00
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:
parent
3ac66fb6d9
commit
1aae926944
3 changed files with 16 additions and 1 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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|网络/)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue