ConceptPortal-public/rsconcept/frontend/src/components/InfoError.tsx

60 lines
1.7 KiB
TypeScript
Raw Normal View History

import axios, { type AxiosError } from 'axios';
import { isResponseHtml } from '@/utils/utils';
2023-07-25 20:27:29 +03:00
2024-01-07 03:29:16 +03:00
import AnimateFade from './AnimateFade';
import PrettyJson from './ui/PrettyJSON';
2023-07-15 17:46:19 +03:00
export type ErrorData = string | Error | AxiosError | undefined;
2023-07-15 17:46:19 +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>;
} 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
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const isHtml = isResponseHtml(error.response);
return (
2023-12-17 20:19:28 +03:00
<div>
<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}
</>
)}
</div>
);
2023-07-15 17:46:19 +03:00
}
function InfoError({ error }: InfoErrorProps) {
2023-07-15 17:46:19 +03:00
return (
2024-01-07 03:29:16 +03:00
<AnimateFade className='px-3 py-2 min-w-[15rem] text-sm font-semibold select-text clr-text-warning'>
2023-12-28 14:04:44 +03:00
<DescribeError error={error} />
2024-01-07 03:29:16 +03:00
</AnimateFade>
2023-12-28 14:04:44 +03:00
);
2023-07-15 17:46:19 +03:00
}
2023-12-28 14:04:44 +03:00
export default InfoError;