generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Components for error boundaries
Signed-off-by: aabidsofi19 <[email protected]>
- Loading branch information
1 parent
a64be50
commit beb5d7e
Showing
3 changed files
with
71 additions
and
1 deletion.
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,68 @@ | ||
import React from 'react'; | ||
import { Button } from '@mui/material'; | ||
import { type FC } from 'react'; | ||
import { | ||
ErrorBoundary as ReactErrorBoundary, | ||
FallbackProps, | ||
ErrorBoundaryProps | ||
} from 'react-error-boundary'; | ||
|
||
const Fallback: React.ComponentType<FallbackProps> = ({ error, resetErrorBoundary }) => { | ||
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> | ||
); | ||
}; | ||
|
||
const reportError = (error: Error, info: React.ErrorInfo) => { | ||
// This is where you'd send the error to Sentry,etc | ||
console.log('Error Caught Inside Boundary --reportError', error, 'Info', info); | ||
}; | ||
|
||
export const ErrorBoundary: FC<ErrorBoundaryProps> = ({ children, ...props }) => { | ||
return ( | ||
<ReactErrorBoundary | ||
FallbackComponent={Fallback as unknown as any} | ||
onError={reportError} | ||
{...props}> | ||
{children} | ||
</ReactErrorBoundary> | ||
); | ||
}; | ||
// | ||
export const withErrorBoundary = ( | ||
Component: FC<any>, | ||
errorHandlingProps: ErrorBoundaryProps | null | ||
) => { | ||
const WrappedWithErrorBoundary = (props: any) => ( | ||
<ErrorBoundary {...((errorHandlingProps ? errorHandlingProps : {}) as any)}> | ||
<Component {...props} /> | ||
</ErrorBoundary> | ||
); | ||
|
||
return WrappedWithErrorBoundary; | ||
}; | ||
|
||
export const withSuppressedErrorBoundary = (Component: React.ComponentType<any>) => { | ||
const WrappedWithErrorBoundary = (props: any) => ( | ||
<ErrorBoundary FallbackComponent={() => null}> | ||
<Component {...props} /> | ||
</ErrorBoundary> | ||
); | ||
|
||
return WrappedWithErrorBoundary; | ||
}; |
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,2 +1,3 @@ | ||
export { default as SearchBar } from './SearchBar'; | ||
export { default as Tooltip } from './Tooltip'; | ||
export { ErrorBoundary, withErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary'; |