generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from sudhanshutech/fix/error-boundar
[custom] Fix and Refactor react-error boundary
- Loading branch information
Showing
6 changed files
with
108 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Link as MuiLink, LinkProps as MuiLinkProps } from '@mui/material'; | ||
|
||
export function Link(props: MuiLinkProps): JSX.Element { | ||
return <MuiLink {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Link } from './Link'; |
156 changes: 87 additions & 69 deletions
156
packages/components/src/custom/ErrorBoundary/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,106 @@ | ||
/* | ||
import { | ||
ErrorBoundaryProps, | ||
FallbackProps, | ||
ErrorBoundary as ReactErrorBoundary | ||
} from 'react-error-boundary'; | ||
import { Button } from '../../base/Button'; | ||
import React from 'react'; | ||
import { styled } from '@mui/system'; | ||
import React, { ComponentType, ReactNode } from 'react'; | ||
import { FallbackProps, ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary'; | ||
import { Box } from '../../base/Box'; | ||
import { Link } from '../../base/Link'; | ||
import { Typography } from '../../base/Typography'; | ||
import { BLACK, KEPPEL } from '../../theme/colors'; | ||
|
||
function Fallback({ error, resetErrorBoundary }: FallbackProps): JSX.Element { | ||
if (error instanceof Error) { | ||
// Check if error is an instance of Error | ||
return ( | ||
<div role="alert"> | ||
<h2>Uh-oh!😔 Please pardon the mesh.</h2> | ||
<div | ||
style={{ | ||
backgroundColor: '#1E2117', | ||
color: '#FFFFFF', | ||
padding: '.85rem', | ||
borderRadius: '.2rem', | ||
marginBlock: '.5rem' | ||
}} | ||
> | ||
<code>{error.message}</code> | ||
</div> | ||
<Button color="primary" variant="contained" onClick={resetErrorBoundary}> | ||
Try again | ||
</Button> | ||
</div> | ||
); | ||
} else { | ||
// Handle the case where error is not an instance of Error | ||
return ( | ||
<div role="alert"> | ||
<h2>Uh-oh!😔 An unknown error occurred.</h2> | ||
<Button color="primary" variant="contained" onClick={resetErrorBoundary}> | ||
Try again | ||
</Button> | ||
const ErrorMessage = styled(Typography)(() => ({ | ||
color: BLACK, | ||
fontWeight: 'normal', | ||
marginTop: '2px', | ||
marginBottom: '2px', | ||
fontSize: '1.15rem' | ||
})); | ||
|
||
const StyledLink = styled(Link)(() => ({ | ||
color: KEPPEL, | ||
textDecoration: 'underline', | ||
cursor: 'pointer' | ||
})); | ||
|
||
interface FallbackComponentProps extends FallbackProps { | ||
resetErrorBoundary: () => void; | ||
children?: ReactNode; | ||
} | ||
|
||
function Fallback({ error, children }: FallbackComponentProps): JSX.Element { | ||
return ( | ||
<div role="alert"> | ||
<h2>Uh-oh!😔 Please pardon the mesh.</h2> | ||
<div | ||
style={{ | ||
backgroundColor: '#1E2117', | ||
color: '#FFFFFF', | ||
padding: '.85rem', | ||
borderRadius: '.2rem', | ||
marginBlock: '.5rem' | ||
}} | ||
> | ||
<code>{(error as Error).message}</code> | ||
</div> | ||
); | ||
} | ||
<ErrorMessage> | ||
We apologize for the inconvenience. The issue may be on our end. If troubleshooting doesn't | ||
work, please check out our support channels{' '} | ||
<StyledLink href="https://discuss.layer5.io/" target="_blank" rel="noopener noreferrer"> | ||
Discuss Forum | ||
</StyledLink>{' '} | ||
{' or '} | ||
<StyledLink href="https://slack.layer5.io/" target="_blank" rel="noopener noreferrer"> | ||
Slack | ||
</StyledLink> | ||
</ErrorMessage> | ||
<Box>{children}</Box> | ||
</div> | ||
); | ||
} | ||
|
||
function reportError(error: Error, info: React.ErrorInfo): void { | ||
// This is where you'd send the error to Sentry, etc. | ||
const reportError = (error: Error, info: { componentStack: string }): void => { | ||
// This is where you'd send the error to Sentry, etc | ||
console.log('Error Caught Inside Boundary --reportError', error, 'Info', info); | ||
}; | ||
|
||
interface ErrorBoundaryProps { | ||
children: ReactNode; | ||
} | ||
|
||
function ErrorBoundary({ children, ...props }: ErrorBoundaryProps): JSX.Element { | ||
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children }) => { | ||
return ( | ||
<ReactErrorBoundary FallbackComponent={Fallback} onError={reportError} {...props}> | ||
<ReactErrorBoundary FallbackComponent={Fallback} onError={reportError}> | ||
{children} | ||
</ReactErrorBoundary> | ||
); | ||
} | ||
function withErrorBoundary<P extends object>( | ||
Component: React.ComponentType<P>, | ||
errorHandlingProps: ErrorBoundaryProps | null | ||
): JSX.Element { | ||
const WrappedWithErrorBoundary = (props: P): JSX.Element => ( | ||
<ErrorBoundary {...(errorHandlingProps ? errorHandlingProps : {})}> | ||
<Component {...props} /> | ||
</ErrorBoundary> | ||
); | ||
return WrappedWithErrorBoundary; | ||
} | ||
}; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
interface WithErrorBoundaryProps { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Component: ComponentType<any>; | ||
errorHandlingProps?: ErrorBoundaryProps; | ||
} | ||
|
||
function withSuppressedErrorBoundary<P extends object>( | ||
Component: React.ComponentType<P> | ||
): JSX.Element { | ||
const WrappedWithErrorBoundary = (props: P & Props): JSX.Element => ( | ||
<ErrorBoundary FallbackComponent={() => null}> | ||
<Component {...props} /> | ||
export const WithErrorBoundary: React.FC<WithErrorBoundaryProps> = ({ | ||
Component, | ||
errorHandlingProps = { children: null } | ||
}: WithErrorBoundaryProps): JSX.Element => { | ||
return ( | ||
<ErrorBoundary {...errorHandlingProps}> | ||
<Component /> | ||
</ErrorBoundary> | ||
); | ||
}; | ||
|
||
return WrappedWithErrorBoundary; | ||
interface WithSuppressedErrorBoundaryProps { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Component: ComponentType<any>; | ||
} | ||
|
||
export { ErrorBoundary, withErrorBoundary, withSuppressedErrorBoundary }; | ||
*/ | ||
export const withSuppressedErrorBoundary: React.FC<WithSuppressedErrorBoundaryProps> = ({ | ||
Component | ||
}: WithSuppressedErrorBoundaryProps): JSX.Element => { | ||
return ( | ||
<ReactErrorBoundary FallbackComponent={Fallback} onError={reportError}> | ||
<Component /> | ||
</ReactErrorBoundary> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
// export { ErrorBoundary, withErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary'; | ||
export { ErrorBoundary, WithErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters