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

Refactor triggerErrorAndCatch function #24181

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 23 additions & 32 deletions fixtures/dom/src/components/fixtures/error-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ function BadRender(props) {
props.doThrow();
}

/**
* Helper function used in TriggerErrorAndCatch & TrySilenceFatalError
* @param container
*/
function triggerErrorAndCatch(container) {
try {
ReactDOM.flushSync(() => {
ReactDOM.render(
<BadRender
doThrow={() => {
throw new Error('Caught error');
}}
/>,
container
);
});
} catch (e) {}
}

class BadDidMount extends React.Component {
componentDidMount() {
this.props.doThrow();
Expand Down Expand Up @@ -73,24 +92,9 @@ class Example extends React.Component {
class TriggerErrorAndCatch extends React.Component {
container = document.createElement('div');

triggerErrorAndCatch = () => {
try {
ReactDOM.flushSync(() => {
ReactDOM.render(
<BadRender
doThrow={() => {
throw new Error('Caught error');
}}
/>,
this.container
);
});
} catch (e) {}
};

render() {
return (
<button onClick={this.triggerErrorAndCatch}>
<button onClick={() => triggerErrorAndCatch(this.container)}>
Trigger error and catch
</button>
);
Expand Down Expand Up @@ -217,25 +221,12 @@ class SilenceRecoverableError extends React.Component {
class TrySilenceFatalError extends React.Component {
container = document.createElement('div');

triggerErrorAndCatch = () => {
try {
ReactDOM.flushSync(() => {
ReactDOM.render(
<BadRender
doThrow={() => {
throw new Error('Caught error');
}}
/>,
this.container
);
});
} catch (e) {}
};

render() {
return (
<SilenceErrors>
<button onClick={this.triggerErrorAndCatch}>Throw fatal error</button>
<button onClick={() => triggerErrorAndCatch(this.container)}>
Throw fatal error
</button>
</SilenceErrors>
);
}
Expand Down