-
Notifications
You must be signed in to change notification settings - Fork 338
refactor(common): convert error-boundary to TypeScript #3964
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
Open
devin-ai-integration
wants to merge
4
commits into
master
Choose a base branch
from
devin-ai/convert-ts-error-boundary-1740441454
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51dc572
refactor(common): convert error-boundary to TypeScript
devin-ai-integration[bot] 9f72078
refactor(common): rename DefaultErrorProps to ErrorComponentProps
devin-ai-integration[bot] 4c3db14
Merge branch 'master' into devin-ai/convert-ts-error-boundary-1740441454
tjuanitas 4b8a9ec
Merge branch 'master' into devin-ai/convert-ts-error-boundary-1740441454
tjuanitas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or 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,20 @@ | ||
| import * as React from 'react'; | ||
| import { FormattedMessage } from 'react-intl'; | ||
| import ErrorMask from '../../../components/error-mask/ErrorMask'; | ||
| import messages from '../messages'; | ||
| import './DefaultError.scss'; | ||
|
|
||
| export interface DefaultErrorProps { | ||
| error?: Error; | ||
| } | ||
|
|
||
| const DefaultError = (): JSX.Element => ( | ||
tjuanitas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <section className="be-default-error"> | ||
| <ErrorMask | ||
| errorHeader={<FormattedMessage {...messages.defaultErrorMaskHeaderMessage} />} | ||
| errorSubHeader={<FormattedMessage {...messages.defaultErrorMaskSubHeaderMessage} />} | ||
| /> | ||
| </section> | ||
| ); | ||
|
|
||
| export default DefaultError; | ||
File renamed without changes.
This file contains hidden or 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,87 @@ | ||
| import * as React from 'react'; | ||
| import noop from 'lodash/noop'; | ||
| import { ERROR_CODE_UNEXPECTED_EXCEPTION, IS_ERROR_DISPLAYED } from '../../../constants'; | ||
| import DefaultError, { DefaultErrorProps } from './DefaultError'; | ||
tjuanitas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import type { ElementsXhrError, ElementsError } from '../../../common/types/api'; | ||
| import type { ElementOrigin } from '../flowTypes'; | ||
|
|
||
| export interface ErrorBoundaryProps { | ||
| children: React.ReactElement; | ||
| errorComponent: React.ComponentType<DefaultErrorProps>; | ||
| errorOrigin: ElementOrigin; | ||
| onError: (error: ElementsError) => void; | ||
| } | ||
|
|
||
| type State = { | ||
| error?: Error; | ||
| }; | ||
|
|
||
| class ErrorBoundary extends React.Component<ErrorBoundaryProps, State> { | ||
| static defaultProps = { | ||
| errorComponent: DefaultError, | ||
| onError: noop, | ||
| }; | ||
|
|
||
| state: State = {}; | ||
|
|
||
| componentDidCatch(error: Error, info: React.ErrorInfo): void { | ||
| this.setState({ error }, () => { | ||
| this.handleError( | ||
| error, | ||
| ERROR_CODE_UNEXPECTED_EXCEPTION, | ||
| { | ||
| ...info, | ||
| }, | ||
| this.props.errorOrigin, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Formats the error and emits it to the top level onError prop | ||
| * | ||
| * @param error - the error which occurred | ||
| * @param code - the error code to identify what error occurred | ||
| * @param contextInfo - additional information which may be useful for the consumer of the error | ||
| * @param origin - the origin of the error | ||
| * @return void | ||
| */ | ||
| handleError = ( | ||
| error: ElementsXhrError | Error, | ||
| code: string, | ||
| contextInfo: Record<string, unknown> = {}, | ||
| origin: ElementOrigin = this.props.errorOrigin, | ||
| ): void => { | ||
| if (!error || !code || !origin) { | ||
| return; | ||
| } | ||
|
|
||
| const elementsError: ElementsError = { | ||
| type: 'error', | ||
| code, | ||
| message: error.message, | ||
| origin, | ||
| context_info: { | ||
| [IS_ERROR_DISPLAYED]: true, | ||
| ...contextInfo, | ||
| }, | ||
| }; | ||
|
|
||
| this.props.onError(elementsError); | ||
| }; | ||
|
|
||
| render(): React.ReactNode { | ||
tjuanitas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const { children, errorComponent: ErrorComponent, ...rest } = this.props; | ||
| const { error } = this.state; | ||
| if (error) { | ||
| return <ErrorComponent error={error} />; | ||
| } | ||
|
|
||
| return React.cloneElement(children, { | ||
| ...rest, | ||
| onError: this.handleError, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default ErrorBoundary; | ||
File renamed without changes.
This file contains hidden or 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,2 @@ | ||
| export { default } from './ErrorBoundary'; | ||
| export { default as withErrorBoundary } from './withErrorBoundary'; |
File renamed without changes.
This file contains hidden or 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,24 @@ | ||
| import * as React from 'react'; | ||
| import DefaultError, { DefaultErrorProps } from './DefaultError'; | ||
| import ErrorBoundary from './ErrorBoundary'; | ||
| import type { ElementOrigin } from '../flowTypes'; | ||
|
|
||
| type ComponentWithRef<P, T = unknown> = React.ComponentType<P & React.RefAttributes<T>>; | ||
|
|
||
| const withErrorBoundary = | ||
| (errorOrigin: ElementOrigin, errorComponent: React.ComponentType<DefaultErrorProps> = DefaultError) => | ||
| <P extends object, T = unknown>(WrappedComponent: ComponentWithRef<P, T>) => { | ||
tjuanitas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const WithErrorBoundaryComponent = React.forwardRef<T, P>((props: P, ref: React.Ref<T>) => ( | ||
| <ErrorBoundary | ||
| errorComponent={errorComponent} | ||
| errorOrigin={errorOrigin} | ||
| {...(props as Record<string, unknown>)} | ||
| > | ||
| <WrappedComponent {...props} ref={ref} /> | ||
| </ErrorBoundary> | ||
| )); | ||
|
|
||
| return WithErrorBoundaryComponent; | ||
tjuanitas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export default withErrorBoundary; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.