27 lines
893 B
TypeScript
Raw Normal View History

2024-12-31 13:55:42 +01:00
import type {AxiosError, AxiosResponse} from 'axios'
import type {MessageInstance, MessageType} from 'antd/lib/message/interface'
2024-12-30 23:50:15 +01:00
import {t} from 'ttag'
2024-08-22 01:44:50 +02:00
export function showErrorAPI(e: AxiosError, messageApi: MessageInstance): MessageType | undefined {
const response = e.response as AxiosResponse
const data = response.data
if ('message' in data) {
return messageApi.error(data.message as string)
}
if (!('detail' in data)) return
const detail = data.detail as string
if (response.status === 429) {
const duration = response.headers['retry-after']
return messageApi.error(t`Please retry after ${duration} seconds`)
}
if (response.status.toString()[0] === '4') {
return messageApi.warning(detail !== '' ? detail : t`An error occurred`)
}
return messageApi.error(detail !== '' ? detail : t`An error occurred`)
2024-12-30 23:50:15 +01:00
}