2023-12-13 14:32:57 +03:00
|
|
|
|
import axios, { type AxiosError } from 'axios';
|
|
|
|
|
|
|
|
|
|
import { isResponseHtml } from '@/utils/utils';
|
2023-07-25 20:27:29 +03:00
|
|
|
|
|
2023-11-27 12:11:39 +03:00
|
|
|
|
import PrettyJson from './Common/PrettyJSON';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
export type ErrorData = string | Error | AxiosError | undefined;
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
interface InfoErrorProps {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
error: ErrorData;
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
function DescribeError({ error }: { error: ErrorData }) {
|
2023-07-15 17:46:19 +03:00
|
|
|
|
if (!error) {
|
|
|
|
|
return <p>Ошибки отсутствуют</p>;
|
|
|
|
|
} else if (typeof error === 'string') {
|
|
|
|
|
return <p>{error}</p>;
|
2023-07-23 15:23:01 +03:00
|
|
|
|
} else if (!axios.isAxiosError(error)) {
|
|
|
|
|
return <PrettyJson data={error} />;
|
|
|
|
|
}
|
|
|
|
|
if (!error?.response) {
|
|
|
|
|
return <p>Нет ответа от сервера</p>;
|
|
|
|
|
}
|
|
|
|
|
if (error.response.status === 404) {
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<div>
|
|
|
|
|
<p>{'Обращение к несуществующему API'}</p>
|
|
|
|
|
<PrettyJson data={error} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
2023-07-25 20:27:29 +03:00
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
|
|
|
|
const isHtml = isResponseHtml(error.response);
|
2023-07-23 15:23:01 +03:00
|
|
|
|
return (
|
2023-12-17 20:19:28 +03:00
|
|
|
|
<div>
|
2023-07-23 15:23:01 +03:00
|
|
|
|
<p className='underline'>Ошибка</p>
|
|
|
|
|
<p>{error.message}</p>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
{error.response.data && (
|
|
|
|
|
<>
|
|
|
|
|
<p className='mt-2 underline'>Описание</p>
|
|
|
|
|
{isHtml ? <div dangerouslySetInnerHTML={{ __html: error.response.data as TrustedHTML }} /> : null}
|
|
|
|
|
{!isHtml ? <PrettyJson data={error.response.data as object} /> : null}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2023-07-23 15:23:01 +03:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
function InfoError({ error }: InfoErrorProps) {
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<div className='px-3 py-2 min-w-[15rem] text-sm font-semibold select-text clr-text-warning'>
|
|
|
|
|
<DescribeError error={error} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default InfoError;
|