ConceptPortal-public/rsconcept/frontend/src/components/info-error.tsx

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-15 12:34:41 +03:00
import clsx from 'clsx';
import { ZodError } from 'zod';
2025-03-12 11:55:43 +03:00
import { type AxiosError, isAxiosError } from '@/backend/api-transport';
import { isResponseHtml } from '@/utils/utils';
2023-07-25 20:27:29 +03:00
2025-03-12 12:04:50 +03:00
import { PrettyJson } from './view';
export type ErrorData = string | Error | AxiosError | ZodError;
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
}
2025-02-19 19:26:29 +03:00
export 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 (error instanceof ZodError) {
return (
2025-02-19 19:26:29 +03:00
<div>
<p>Ошибка валидации данных</p>
<PrettyJson data={JSON.parse(error.toString()) as unknown} />;
</div>
);
} else if (!isAxiosError(error)) {
return (
2025-02-19 19:26:29 +03:00
<div>
<p>
<b>Error:</b> {error.name}
</p>
<p>
<b>Message:</b> {error.message}
</p>
{error.stack && <pre className='whitespace-pre-wrap p-2 overflow-x-auto break-words'>{error.stack}</pre>}
</div>
);
}
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
}
if (error.response.status === 403 && error.message.includes('CSRF')) {
return (
<div>
<p>{'Соединение с сервером потеряно. Перезагрузите страницу'}</p>
<PrettyJson data={error} />
</div>
);
}
2023-07-25 20:27:29 +03:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
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
}
export function InfoError({ error }: InfoErrorProps) {
2023-07-15 17:46:19 +03:00
return (
<div
2024-03-15 12:34:41 +03:00
className={clsx(
2025-04-29 13:33:39 +03:00
'min-w-100', //
2024-03-15 12:34:41 +03:00
'px-3 py-2 flex flex-col',
'text-destructive text-sm font-semibold',
2024-03-15 12:34:41 +03:00
'select-text'
)}
>
<div className='font-normal text-foreground mb-6'>
2024-05-20 17:45:37 +03:00
<p>Пожалуйста сделайте скриншот и отправьте вместе с описанием ситуации на почту portal@acconcept.ru</p>
2024-03-15 12:34:41 +03:00
<br />
2024-05-20 17:45:37 +03:00
<p>Для продолжения работы перезагрузите страницу</p>
</div>
2023-12-28 14:04:44 +03:00
<DescribeError error={error} />
</div>
2023-12-28 14:04:44 +03:00
);
2023-07-15 17:46:19 +03:00
}