Portal/rsconcept/frontend/src/components/InfoError.tsx

101 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import clsx from 'clsx';
import { AxiosError, isAxiosError } from '@/backend/apiTransport';
2024-06-07 20:17:03 +03:00
import { isResponseHtml } from '@/utils/utils';
import { PrettyJson } from './View';
2025-01-23 19:41:31 +03:00
export type ErrorData = string | Error | AxiosError | undefined | null;
2024-06-07 20:17:03 +03:00
interface InfoErrorProps {
error: ErrorData;
}
function DescribeError({ error }: { error: ErrorData }) {
if (!error) {
return <p>Ошибки отсутствуют</p>;
} else if (typeof error === 'string') {
return <p>{error}</p>;
} else if (!isAxiosError(error)) {
return (
<div className='mt-6'>
<p>
<b>Error:</b> {error.name}
</p>
<p>
<b>Message:</b> {error.message}
</p>
{error.stack && (
<pre
style={{
whiteSpace: 'pre-wrap',
wordWrap: 'break-word',
padding: '6px',
overflowX: 'auto'
}}
>
{error.stack}
</pre>
)}
</div>
);
2024-06-07 20:17:03 +03:00
}
if (!error?.response) {
return <p>Нет ответа от сервера</p>;
}
if (error.response.status === 404) {
return (
<div>
<p>{'Обращение к несуществующему API'}</p>
<PrettyJson data={error} />
</div>
);
}
if (error.response.status === 403 && error.message.includes('CSRF')) {
return (
<div>
<p>{'Соединение с сервером потеряно. Перезагрузите страницу'}</p>
<PrettyJson data={error} />
</div>
);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const isHtml = isResponseHtml(error.response);
return (
<div>
<p className='underline'>Ошибка</p>
<p>{error.message}</p>
{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>
);
}
export function InfoError({ error }: InfoErrorProps) {
2024-06-07 20:17:03 +03:00
return (
2024-12-12 13:17:24 +03:00
<div
2024-06-07 20:17:03 +03:00
className={clsx(
2024-12-12 13:17:24 +03:00
'cc-fade-in',
2024-09-21 20:03:49 +03:00
'min-w-[25rem]',
2024-06-07 20:17:03 +03:00
'px-3 py-2 flex flex-col',
'text-warn-600 text-sm font-semibold',
2024-06-07 20:17:03 +03:00
'select-text'
)}
>
<div className='font-normal clr-text-default'>
<p>Пожалуйста сделайте скриншот и отправьте вместе с описанием ситуации на почту portal@acconcept.ru</p>
<br />
<p>Для продолжения работы перезагрузите страницу</p>
</div>
<DescribeError error={error} />
2024-12-12 13:17:24 +03:00
</div>
2024-06-07 20:17:03 +03:00
);
}