2023-07-25 20:27:29 +03:00
|
|
|
|
import axios, { type AxiosError } from 'axios';
|
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
import PrettyJson from './Common/PrettyJSON';
|
|
|
|
|
|
|
|
|
|
export type ErrorInfo = string | Error | AxiosError | undefined;
|
|
|
|
|
|
|
|
|
|
interface BackendErrorProps {
|
|
|
|
|
error: ErrorInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DescribeError(error: ErrorInfo) {
|
2023-07-23 15:23:01 +03:00
|
|
|
|
console.log(error);
|
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 (
|
|
|
|
|
<div className='flex flex-col justify-start'>
|
2023-07-25 20:27:29 +03:00
|
|
|
|
<p>{'Обращение к несуществующему API'}</p>
|
2023-07-23 15:23:01 +03:00
|
|
|
|
<PrettyJson data={error} />
|
2023-07-15 17:46:19 +03:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-25 20:27:29 +03:00
|
|
|
|
|
2023-07-23 15:23:01 +03:00
|
|
|
|
const isHtml = error.response.headers['content-type'].includes('text/html');
|
|
|
|
|
return (
|
|
|
|
|
<div className='flex flex-col justify-start'>
|
|
|
|
|
<p className='underline'>Ошибка</p>
|
|
|
|
|
<p>{error.message}</p>
|
|
|
|
|
{error.response.data && (<>
|
|
|
|
|
<p className='mt-2 underline'>Описание</p>
|
|
|
|
|
{ isHtml && <div dangerouslySetInnerHTML={{ __html: error.response.data }} /> }
|
|
|
|
|
{ !isHtml && <PrettyJson data={error.response.data} />}
|
|
|
|
|
</>)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
|
function BackendError({ error }: BackendErrorProps) {
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
|
|
|
|
<div className='py-2 text-sm font-semibold text-red-600 dark:text-red-400'>
|
|
|
|
|
{DescribeError(error)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
|
export default BackendError;
|