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

[custom] Fix and Refactor react-error boundary #340

Merged
merged 8 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"lint:fix": "eslint --fix"
},
"dependencies": {
"@layer5/sistent-svg": "^0.14.0"
"@layer5/sistent-svg": "^0.14.0",
"react-error-boundary": "^4.0.11"
},
"devDependencies": {
"@emotion/react": "^11.11.1",
Expand Down
126 changes: 58 additions & 68 deletions packages/components/src/custom/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,78 @@
/*
import {
ErrorBoundaryProps,
FallbackProps,
ErrorBoundary as ReactErrorBoundary
} from 'react-error-boundary';
import React, { ComponentType, ReactNode } from 'react';
import { FallbackProps, ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary';
import { Button } from '../../base/Button';
import React from 'react';

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>
interface FallbackComponentProps extends FallbackProps {
resetErrorBoundary: () => void;
}

function Fallback({ error, resetErrorBoundary }: 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>
);
}
<Button color="primary" variant="contained" onClick={resetErrorBoundary}>
Try again
</Button>
</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>
);
};
2 changes: 1 addition & 1 deletion packages/components/src/custom/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// export { ErrorBoundary, withErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary';
export { ErrorBoundary, WithErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary';
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ __metadata:
notistack: ^3.0.1
react: ^17.0.0 || ^18.0.0
react-dom: ^17.0.0 || ^18.0.0
react-error-boundary: ^4.0.11
tsconfig: "workspace:^"
tsup: ^7.2.0
typescript: ^5.0.2
Expand Down Expand Up @@ -8562,6 +8563,17 @@ __metadata:
languageName: node
linkType: hard

"react-error-boundary@npm:^4.0.11":
version: 4.0.11
resolution: "react-error-boundary@npm:4.0.11"
dependencies:
"@babel/runtime": ^7.12.5
peerDependencies:
react: ">=16.13.1"
checksum: b3c157fea4e8f78411e9aa0fbf5241f6907b66ede1cd8b7bb22faaeb0339ebeb3dc8e63bf90ef3f740bfa8fd994ca6edf975089cd371b664ad6c2735e7512d38
languageName: node
linkType: hard

"react-is@npm:^16.13.1, react-is@npm:^16.7.0":
version: 16.13.1
resolution: "react-is@npm:16.13.1"
Expand Down
Loading