Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error boundary #5003

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { queryClient } from '@app/common/persistence';
import { ThemeSwitcherProvider } from '@app/common/theme-provider';
import { FullPageLoadingSpinner } from '@app/components/loading-spinner';
import { Devtools } from '@app/features/devtool/devtools';
import { AppErrorBoundary } from '@app/features/errors/app-error-boundary';
import { HeadProvider } from '@app/features/html-head/head-provider';
import { ToastsProvider } from '@app/features/toasts/toasts-provider';
import { AppRoutes } from '@app/routes/app-routes';
Expand Down Expand Up @@ -42,9 +41,7 @@ function ConnectedApp() {
}}
>
<Suspense fallback={<FullPageLoadingSpinner />}>
<AppErrorBoundary>
<AppRoutes />
</AppErrorBoundary>
<AppRoutes />
{reactQueryDevToolsEnabled && <Devtools />}
</Suspense>
</LeatherQueryProvider>
Expand Down
4 changes: 2 additions & 2 deletions src/app/common/clarity-prism.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as PrismLib from 'prismjs';

interface PrismType {
export interface PrismType {
languages: typeof PrismLib.languages;
tokenize: typeof PrismLib.tokenize;
highlight: typeof PrismLib.highlight;
Expand Down Expand Up @@ -123,4 +123,4 @@ buff|hash160|sha256|sha512|sha512/256|keccak256|true|false|none)' +

clarity(PrismLib);

export const Prism = PrismLib;
export const Prism = PrismLib as PrismType;
152 changes: 108 additions & 44 deletions src/app/features/errors/app-error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,122 @@
import { Box, Stack, styled } from 'leather-styles/jsx';
import { useRouteError } from 'react-router-dom';

import BroadcastError from '@assets/images/unhappy-face-ui.png';
import { isError } from '@leather-wallet/utils';
import { SharedComponentsSelectors } from '@tests/selectors/shared-component.selectors';
import { Box, Flex, HStack, styled } from 'leather-styles/jsx';

import { Prism } from '@app/common/clarity-prism';
import { HasChildren } from '@app/common/has-children';
import { ErrorBoundary, FallbackProps, useErrorHandler } from '@app/features/errors/error-boundary';
import { openGithubIssue } from '@app/features/errors/utils';
import { useErrorStackTraceState } from '@app/store/ui/ui.hooks';
import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard';
import { Button } from '@app/ui/components/button/button';
import { CodeBlock } from '@app/ui/components/codeblock';
import { Title } from '@app/ui/components/typography/title';
import { Link } from '@app/ui/components/link/link';
import { CopyIcon } from '@app/ui/icons';

function ErrorFallback({ error, resetErrorBoundary }: FallbackProps) {
const [value] = useErrorStackTraceState();
import { useToast } from '../toasts/use-toast';

function ErroBoundaryBody() {
return (
<Stack gap="space.06" flexGrow={1}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please refactor this so it's a dumb component, receiving some props it needs and located in the ui/components folder?

We should also add a storybook story for it so we can see how it looks

<Title>Something went wrong</Title>
<Box className="error-codeblock" maxWidth="100vw" overflow="hidden">
{value && (
<CodeBlock
border="default"
code={value}
flexShrink={1}
overflow="auto"
language="bash"
maxHeight="305px"
maxWidth="100%"
prism={Prism as any}
width="100%"
/>
)}
</Box>
<Stack mt="auto" gap="space.04">
<styled.button onClick={resetErrorBoundary} type="button">
Reload extension
</styled.button>
<styled.button
onClick={() => openGithubIssue({ message: error.message, stackTrace: value })}
type="button"
<styled.span
data-testid={SharedComponentsSelectors.BroadcastErrorTitle}
mx="space.05"
mt="space.05"
textStyle="label.02"
>
Leather has crashed. If this problem persists, contact our{' '}
<Link
href="https://leather.gitbook.io/guides/installing/contact-support"
target="_blank"
textDecoration="underline"
>
<styled.span
data-testid={SharedComponentsSelectors.BroadcastErrorTitle}
textStyle="label.02"
>
Report issue on GitHub
</styled.button>
</Stack>
</Stack>
support team.
</styled.span>
</Link>
</styled.span>
);
}

export function AppErrorBoundary({ children }: HasChildren) {
const handleOnError = useErrorHandler();
const title = 'Something went wrong';

const defaultErrorText = 'Unknown error';

function getErrorText(error: unknown) {
if (!isError(error)) return defaultErrorText;
return error.stack || defaultErrorText;
}

export function RouterErrorBoundary() {
const error = useRouteError();
const toast = useToast();

const errorText = getErrorText(error);

const { onCopy } = useClipboard(getErrorText(error));

function onClickCopy() {
onCopy();
toast.success('Error copied!');
}

return (
<ErrorBoundary
onReset={() => window.location.reload()}
FallbackComponent={ErrorFallback}
onError={handleOnError}
<Flex
alignItems="center"
justifyContent="center"
flexDirection="column"
px={['space.05', 'unset']}
py={['space.05', 'space.06']}
width="100%"
maxWidth="500px"
>
{children}
</ErrorBoundary>
<Box mt="space.05">
<img src={BroadcastError} alt="Unhappy user interface cloud" width="106px" />
</Box>

<styled.span
data-testid={SharedComponentsSelectors.BroadcastErrorTitle}
mx="space.05"
mt="space.05"
textStyle="heading.05"
>
{title}
</styled.span>

<ErroBoundaryBody />

{errorText && (
<CodeBlock
bg="ink.background-secondary"
borderRadius="sm"
my="space.05"
mx="space.05"
p="space.04"
prism={Prism}
code={errorText}
language="json"
hideLineHover
/>
)}

<HStack width="100%" gap="space.04">
<Button
flex="1"
display="flex"
justifyContent="center"
alignItems="center"
gap="5"
onClick={onClickCopy}
variant="outline"
>
Copy error
<CopyIcon />
</Button>
<Button flex="1" onClick={() => window.location.reload()}>
Reload extension
</Button>
</HStack>
</Flex>
);
}
154 changes: 0 additions & 154 deletions src/app/features/errors/error-boundary.tsx

This file was deleted.

Loading
Loading