-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add
shouldCatch
prop on ErrorBoundary (#569)
fix #567 fix #564 fix #450 Specially thanks to @sungh0lim @tooooo1 # new prop of ErrorBoundary: `shoudCatch` <!-- A clear and concise description of what this pr is about. -->  ```tsx import { isAxios } from 'axios' class CustomError extends Error { // ... } const Example = () => { const isTrueRandomly = Math.random() > 0.5 return ( <> // We can catch them by shouldCatch <ErrorBoundary shouldCatch={CustomError}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={isAxiosError}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={isTrueRandomly}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={[CustomError, isAxiosError, isTrueRandomly]}> <Child /> </ErrorBoundary> // We can ignore them by reversing shouldCatch <ErrorBoundary shouldCatch={(error) => !(error instanceof CustomError)}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={(error) => !isAxiosError(error)}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={!isTrueRandomly}> <Child /> </ErrorBoundary> <ErrorBoundary shouldCatch={[(error) => !(error instanceof CustomError), (error) => isAxiosError(error), !isTrueRandomly]}> <Child /> </ErrorBoundary> </> ) } ``` ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Minsoo Kim <[email protected]> Co-authored-by: Sungho Lim <[email protected]> Co-authored-by: Chungil Jung <[email protected]>
- Loading branch information
1 parent
97faf61
commit caed129
Showing
14 changed files
with
306 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@suspensive/react': minor | ||
--- | ||
|
||
feat(react): add `shouldCatch` prop of 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class CustomError extends Error { | ||
constructor(...args: ConstructorParameters<ErrorConstructor>) { | ||
super(...args) | ||
console.error(...args) | ||
} | ||
} |
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 @@ | ||
export class CustomNotError { | ||
constructor(public message?: string) { | ||
console.log(message) | ||
} | ||
} |
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
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,38 @@ | ||
import { CustomError, CustomNotError } from '@suspensive/test-utils' | ||
import type { ComponentProps } from 'react' | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { ErrorBoundary } from './ErrorBoundary' | ||
import type { ConstructorType } from './utility-types' | ||
|
||
describe('<ErrorBoundary/>', () => { | ||
it('should pass only boolean or ErrorConstructor or ShouldCatchCallback or ShouldCatch[]', () => { | ||
type ShouldCatchCallback = (error: Error) => boolean | ||
type ShouldCatch = boolean | ConstructorType<Error> | ShouldCatchCallback | ||
expectTypeOf<ComponentProps<typeof ErrorBoundary>['shouldCatch']>().toEqualTypeOf< | ||
undefined | ShouldCatch | [ShouldCatch, ...ShouldCatch[]] | ||
>() | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const ShouldCatchByMany = () => ( | ||
<ErrorBoundary | ||
shouldCatch={[ | ||
// @ts-expect-error CustomNotError should be new (...args) => Error | ||
CustomNotError, | ||
CustomError, | ||
(error) => error instanceof CustomError, | ||
Math.random() > 0.5, | ||
]} | ||
fallback={({ error }) => <>{error.message} of Child</>} | ||
></ErrorBoundary> | ||
) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const ShouldCatchByOne = () => ( | ||
<ErrorBoundary | ||
// @ts-expect-error CustomNotError should be new (...args) => Error | ||
shouldCatch={CustomNotError} | ||
fallback={({ error }) => <>{error.message} of Child</>} | ||
></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
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 type ConstructorType<TClass> = new (...args: any[]) => TClass |
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 type { PropsWithDevMode } from './PropsWithDevMode' | ||
export type { OmitKeyOf } from './OmitKeyOf' | ||
export type { ConstructorType } from './ConstructorType' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
caed129
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs-v1 β ./docs/v1
docs-v1-suspensive.vercel.app
v1.suspensive.org
docs-v1-git-main-suspensive.vercel.app
www.suspensive.org
docs-suspensive.vercel.app
suspensive.org
caed129
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
visualization β ./websites/visualization
visualization-suspensive.vercel.app
visualization-git-main-suspensive.vercel.app
visualization.suspensive.org
caed129
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs-v2 β ./docs/v2
docs-v2-suspensive.vercel.app
docs-v2-sigma.vercel.app
docs-v2-git-main-suspensive.vercel.app
v2.suspensive.org