Skip to content

Commit

Permalink
feat(): Update error boundaries components
Browse files Browse the repository at this point in the history
Signed-off-by: yash sharma <[email protected]>
  • Loading branch information
Yashsharma1911 committed Feb 12, 2024
1 parent f71f14c commit 46b5e88
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/custom/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FallbackComponentProps extends FallbackProps {
children?: React.ReactNode;
}

function Fallback({ error, children }: FallbackComponentProps): JSX.Element {
export function Fallback({ error, children }: FallbackComponentProps): JSX.Element {
return (
<div role="alert">
<h2>Uh-oh!😔 Please pardon the mesh.</h2>
Expand Down Expand Up @@ -62,12 +62,13 @@ const reportError = (error: Error, info: React.ErrorInfo): void => {
};

interface ErrorBoundaryProps {
customFallback?: React.ComponentType<FallbackProps>;
children: React.ReactNode;
}

export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children }) => {
export const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ customFallback, children }) => {
return (
<ReactErrorBoundary FallbackComponent={Fallback} onError={reportError}>
<ReactErrorBoundary FallbackComponent={customFallback ?? Fallback} onError={reportError}>
{children}
</ReactErrorBoundary>
);
Expand Down Expand Up @@ -99,7 +100,7 @@ export const withSuppressedErrorBoundary: React.FC<WithSuppressedErrorBoundaryPr
Component
}: WithSuppressedErrorBoundaryProps): JSX.Element => {
return (
<ReactErrorBoundary FallbackComponent={Fallback} onError={reportError}>
<ReactErrorBoundary FallbackComponent={() => null} onError={reportError}>
<Component />
</ReactErrorBoundary>
);
Expand Down
85 changes: 85 additions & 0 deletions src/custom/ErrorBoundary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## Error Boundary Components

### `ErrorBoundary`

The `ErrorBoundary` component is designed to catch errors that occur within its child components and provide a customizable fallback UI when an error occurs.

#### Usage

Wrap your component with the `ErrorBoundary`:

```tsx
import { ErrorBoundary } from '@layer5/sistent';

const MyComponent = () => {
// Your component logic

return <ErrorBoundary>{/* Your component JSX */}</ErrorBoundary>;
};
```

##### Custom Fallback

You can provide a custom fallback component to `ErrorBoundary`:

```tsx
const MyComponent = () => {
// Your component logic

return (
<ErrorBoundary customFallback={CustomFallbackComponent}>
{/* Your component JSX */}
</ErrorBoundary>
);
};
```

### `withErrorBoundary`

`withErrorBoundary` is a higher-order component (HOC) that simplifies wrapping a component with ErrorBoundary. It uses default fallback component. This can be useFul to wrap child components

#### Usage

Wrap your component using `withErrorBoundary`:

```tsx
import { withErrorBoundary } from '@layer5/sistent';

const MyComponent = withErrorBoundary(() => {
return {
/* Your component JSX */
};
});
```

### `withSuppressedErrorBoundary`

`withSuppressedErrorBoundary` is another HOC that suppresses the error in browser's console instead of displaying fallback component to users, this can be useFull for errors that are not critical and can be avoided.

#### Usage

Wrap your component using withSuppressedErrorBoundary:

```tsx
import { withSuppressedErrorBoundary } from '@layer5/sistent';

const MyComponent = withSuppressedErrorBoundary(() => {
return {
/* Your component JSX */
};
});
```

### Handling Different Levels of Errors

#### Critical Errors

Critical errors typically originate from parent or root components and can potentially lead to the entire page crashing. In such cases, it is recommended to use the ErrorBoundary with either the default fallback component or a custom fallback component to ensure users receive assistance.

#### Non-critical Errors

Non-critical errors occur in child components and may not result in a page crash or hinder users from performing other operations. In these cases, displaying the error through a toaster notification or handling it as an event can be beneficial.

#### Errors That Can Be Avoided

In some scenarios, a child component might encounter an error that doesn't block users and doesn't require immediate attention. Such errors can be avoided and suppressed into the browser's console for debugging purposes. The `withSuppressedErrorBoundary` higher-order component (HOC) function can be useful in this scenario.
7 changes: 6 additions & 1 deletion src/custom/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { ErrorBoundary, WithErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary';
export {
ErrorBoundary,
Fallback,
WithErrorBoundary,
withSuppressedErrorBoundary
} from './ErrorBoundary';
8 changes: 7 additions & 1 deletion src/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
CustomColumnVisibilityControlProps
} from './CustomColumnVisibilityControl/CustomColumnVisibilityControl';
import { EmptyState } from './EmptyState';
import { ErrorBoundary, WithErrorBoundary, withSuppressedErrorBoundary } from './ErrorBoundary';
import {
ErrorBoundary,
Fallback,
WithErrorBoundary,
withSuppressedErrorBoundary
} from './ErrorBoundary';
import { FlipCard } from './FlipCard';
import { useWindowDimensions } from './Helpers/Dimension';
import { useNotificationHandler } from './Helpers/Notification';
Expand All @@ -22,6 +27,7 @@ export {
CustomColumnVisibilityControl,
EmptyState,
ErrorBoundary,
Fallback,
FlipCard,
PopperListener,
ResponsiveDataTable,
Expand Down

0 comments on commit 46b5e88

Please sign in to comment.