mirror of
https://github.com/inventree/InvenTree.git
synced 2026-07-17 20:23:50 +00:00
[UI] Display error information in boundary (#12403)
* Display error information in boundary * Fix SecurityContext
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
import { t } from '@lingui/core/macro';
|
import { t } from '@lingui/core/macro';
|
||||||
import { Alert, Stack, Text } from '@mantine/core';
|
import { Alert, Stack, Text } from '@mantine/core';
|
||||||
import { ErrorBoundary, type FallbackRender } from '@sentry/react';
|
import { ErrorBoundary, type FallbackRender } from '@sentry/react';
|
||||||
import { IconExclamationCircle } from '@tabler/icons-react';
|
import { IconExclamationCircle, IconInfoCircle } from '@tabler/icons-react';
|
||||||
import { type ReactNode, useCallback } from 'react';
|
import { type ReactNode, useCallback, useState } from 'react';
|
||||||
|
|
||||||
export function DefaultFallback({
|
export function DefaultFallback({
|
||||||
title
|
title,
|
||||||
}: Readonly<{ title: string }>): ReactNode {
|
error
|
||||||
|
}: Readonly<{ title: string; error: string | null }>): ReactNode {
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<Alert
|
<Alert
|
||||||
color='red'
|
color='red'
|
||||||
icon={<IconExclamationCircle />}
|
icon={<IconExclamationCircle />}
|
||||||
@@ -15,13 +17,19 @@ export function DefaultFallback({
|
|||||||
>
|
>
|
||||||
<Stack gap='xs'>
|
<Stack gap='xs'>
|
||||||
<Text size='sm'>
|
<Text size='sm'>
|
||||||
{t`An error occurred while rendering this component. Refer to the console for more information.`}
|
{t`An error occurred while rendering this component.`}
|
||||||
</Text>
|
</Text>
|
||||||
<Text size='sm'>
|
<Text size='sm'>
|
||||||
{t`Try reloading the page, or contact your administrator if the problem persists.`}
|
{t`Try reloading the page, or contact your administrator if the problem persists.`}
|
||||||
</Text>
|
</Text>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Alert>
|
</Alert>
|
||||||
|
{error && (
|
||||||
|
<Alert color='red' icon={<IconInfoCircle />} title={t`Error Details`}>
|
||||||
|
<Text size='sm'>{error}</Text>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,17 +42,22 @@ export function Boundary({
|
|||||||
label: string;
|
label: string;
|
||||||
fallback?: React.ReactElement<any> | FallbackRender;
|
fallback?: React.ReactElement<any> | FallbackRender;
|
||||||
}>): ReactNode {
|
}>): ReactNode {
|
||||||
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||||
|
|
||||||
const onError = useCallback(
|
const onError = useCallback(
|
||||||
(error: unknown, componentStack: string | undefined, eventId: string) => {
|
(error: unknown, componentStack: string | undefined, eventId: string) => {
|
||||||
console.error(`ERR: Error rendering component: ${label}`);
|
console.error(`ERR: Error rendering component: ${label}`);
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
setErrorMessage(error instanceof Error ? error.message : String(error));
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ErrorBoundary
|
<ErrorBoundary
|
||||||
fallback={fallback ?? <DefaultFallback title={label} />}
|
fallback={
|
||||||
|
fallback ?? <DefaultFallback title={label} error={errorMessage} />
|
||||||
|
}
|
||||||
onError={onError}
|
onError={onError}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -45,9 +45,12 @@ export function SecurityContent() {
|
|||||||
|
|
||||||
const user = useUserState();
|
const user = useUserState();
|
||||||
|
|
||||||
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||||
|
|
||||||
const onError = useCallback(
|
const onError = useCallback(
|
||||||
(error: unknown, componentStack: string | undefined, eventId: string) => {
|
(error: unknown, componentStack: string | undefined, eventId: string) => {
|
||||||
console.error(`ERR: Error rendering component: ${error}`);
|
console.error(`ERR: Error rendering component: ${error}`);
|
||||||
|
setErrorMessage(error instanceof Error ? error.message : String(error));
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
@@ -95,7 +98,9 @@ export function SecurityContent() {
|
|||||||
</Accordion.Control>
|
</Accordion.Control>
|
||||||
<Accordion.Panel>
|
<Accordion.Panel>
|
||||||
<ErrorBoundary
|
<ErrorBoundary
|
||||||
fallback={<DefaultFallback title={'API Table'} />}
|
fallback={
|
||||||
|
<DefaultFallback title={'API Table'} error={errorMessage} />
|
||||||
|
}
|
||||||
onError={onError}
|
onError={onError}
|
||||||
>
|
>
|
||||||
<ApiTokenTable only_myself />
|
<ApiTokenTable only_myself />
|
||||||
|
|||||||
Reference in New Issue
Block a user