feat: improve getApiErrorMessage with user-friendly messages
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { getApiErrorMessage } from '../get-api-error-message'
|
||||||
|
|
||||||
|
describe('getApiErrorMessage', () => {
|
||||||
|
it('returns server error message from response.data.error', () => {
|
||||||
|
const error = {
|
||||||
|
isAxiosError: true,
|
||||||
|
response: { data: { error: 'Товар не найден' }, status: 404 },
|
||||||
|
}
|
||||||
|
expect(getApiErrorMessage(error)).toBe('Товар не найден')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns network error message when no response', () => {
|
||||||
|
const error = { isAxiosError: true, response: undefined }
|
||||||
|
expect(getApiErrorMessage(error)).toBe('Нет соединения с сервером. Проверьте подключение к интернету.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns server error message for 5xx status', () => {
|
||||||
|
const error = { isAxiosError: true, response: { data: {}, status: 500 } }
|
||||||
|
expect(getApiErrorMessage(error)).toBe('Произошла ошибка. Попробуйте повторить позже.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns error message for Error instance', () => {
|
||||||
|
expect(getApiErrorMessage(new Error('Something broke'))).toBe('Something broke')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns unknown error for falsy input', () => {
|
||||||
|
expect(getApiErrorMessage(null)).toBe('Произошла неизвестная ошибка')
|
||||||
|
expect(getApiErrorMessage(undefined)).toBe('Произошла неизвестная ошибка')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns unknown error for random object', () => {
|
||||||
|
expect(getApiErrorMessage({ foo: 'bar' })).toBe('Произошла неизвестная ошибка')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,8 +1,23 @@
|
|||||||
export function getApiErrorMessage(err: unknown): string | null {
|
import { isAxiosError } from 'axios'
|
||||||
if (!err || typeof err !== 'object') return null
|
|
||||||
const anyErr = err as Record<string, unknown>
|
export function getApiErrorMessage(error: unknown): string {
|
||||||
const response = anyErr.response as Record<string, unknown> | undefined
|
if (!error) return 'Произошла неизвестная ошибка'
|
||||||
const data = response?.data as Record<string, unknown> | undefined
|
|
||||||
const msg = data?.error
|
if (isAxiosError(error)) {
|
||||||
return typeof msg === 'string' ? msg : null
|
if (error.response?.data?.error && typeof error.response.data.error === 'string') {
|
||||||
|
return error.response.data.error
|
||||||
|
}
|
||||||
|
if (!error.response) {
|
||||||
|
return 'Нет соединения с сервером. Проверьте подключение к интернету.'
|
||||||
|
}
|
||||||
|
if (error.response.status >= 500) {
|
||||||
|
return 'Произошла ошибка. Попробуйте повторить позже.'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error instanceof Error) {
|
||||||
|
return error.message
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Произошла неизвестная ошибка'
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user