import axios, { type AxiosError,AxiosHeaderValue } from 'axios';
import PrettyJson from './Common/PrettyJSON';
export type ErrorInfo = string | Error | AxiosError | undefined;
interface BackendErrorProps {
error: ErrorInfo
}
function DescribeError(error: ErrorInfo) {
reportError(error);
if (!error) {
return
Ошибки отсутствуют
;
} else if (typeof error === 'string') {
return {error}
;
} else if (!axios.isAxiosError(error)) {
return ;
}
if (!error?.response) {
return Нет ответа от сервера
;
}
if (error.response.status === 404) {
return (
{'Обращение к несуществующему API'}
);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const isHtml = (() => {
if (!error.response) {
return false;
}
const header = error.response.headers['content-type'] as AxiosHeaderValue;
if (!header) {
return false;
}
if (typeof header === 'number' || typeof header === 'boolean') {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
return header.includes('text/html');
})();
return (
Ошибка
{error.message}
{error.response.data && (<>
Описание
{isHtml ?
: null}
{!isHtml ?
: null}
>)}
);
}
function BackendError({ error }: BackendErrorProps) {
return (
{DescribeError(error)}
);
}
export default BackendError;