mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
refactor: move functions
This commit is contained in:
27
assets/utils/functions/showErrorAPI.tsx
Normal file
27
assets/utils/functions/showErrorAPI.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import {AxiosError, AxiosResponse} from "axios";
|
||||
import {MessageInstance, MessageType} from "antd/lib/message/interface";
|
||||
import {t} from "ttag";
|
||||
|
||||
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`)
|
||||
}
|
||||
Reference in New Issue
Block a user