-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
[compiler][fixtures] Patch error-handling edge case in snap evaluator #31082
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,38 +60,55 @@ const ExportSchema = z.object({ | |
FIXTURE_ENTRYPOINT: EntrypointSchema, | ||
}); | ||
|
||
const NO_ERROR_SENTINEL = Symbol(); | ||
/** | ||
* Wraps WrapperTestComponent in an error boundary to simplify re-rendering | ||
* when an exception is thrown. | ||
* A simpler alternative may be to re-mount test components manually. | ||
*/ | ||
class WrapperTestComponentWithErrorBoundary extends React.Component< | ||
{fn: any; params: Array<any>}, | ||
{hasError: boolean; error: any} | ||
{errorFromLastRender: any} | ||
> { | ||
propsErrorMap: MutableRefObject<Map<any, any>>; | ||
/** | ||
* Limit retries of the child component by caching seen errors. | ||
*/ | ||
propsErrorMap: Map<any, any>; | ||
lastProps: any | null; | ||
// lastProps: object | null; | ||
constructor(props: any) { | ||
super(props); | ||
this.state = {hasError: false, error: null}; | ||
this.propsErrorMap = React.createRef() as MutableRefObject<Map<any, any>>; | ||
this.propsErrorMap.current = new Map(); | ||
this.lastProps = null; | ||
this.propsErrorMap = new Map<any, any>(); | ||
this.state = { | ||
errorFromLastRender: NO_ERROR_SENTINEL, | ||
}; | ||
} | ||
static getDerivedStateFromError(error: any) { | ||
return {hasError: true, error: error}; | ||
// Reschedule a second render that immediately returns the cached error | ||
return {errorFromLastRender: error}; | ||
} | ||
override componentDidUpdate() { | ||
if (this.state.hasError) { | ||
this.setState({hasError: false, error: null}); | ||
if (this.state.errorFromLastRender !== NO_ERROR_SENTINEL) { | ||
// Reschedule a third render that immediately returns the cached error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't get the comment here, seems backwards since we're clearing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wouldn't return Definitely not sold that this is the best way writing this and I'm probably missing a common pattern, any suggestions welcome. |
||
this.setState({errorFromLastRender: NO_ERROR_SENTINEL}); | ||
} | ||
} | ||
override render() { | ||
if (this.state.hasError) { | ||
this.propsErrorMap.current!.set( | ||
this.props, | ||
`[[ (exception in render) ${this.state.error?.toString()} ]]`, | ||
); | ||
if ( | ||
this.state.errorFromLastRender !== NO_ERROR_SENTINEL && | ||
this.props === this.lastProps | ||
) { | ||
/** | ||
* The last render errored, cache the error message to avoid running the | ||
* test fixture more than once | ||
*/ | ||
const errorMsg = `[[ (exception in render) ${this.state.errorFromLastRender?.toString()} ]]`; | ||
this.propsErrorMap.set(this.lastProps, errorMsg); | ||
return errorMsg; | ||
} | ||
const cachedError = this.propsErrorMap.current!.get(this.props); | ||
this.lastProps = this.props; | ||
const cachedError = this.propsErrorMap.get(this.props); | ||
if (cachedError != null) { | ||
return cachedError; | ||
} | ||
|
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.
Note that we now correctly do the non-throwing re-render instead of returning a cached error