Skip to content

Commit

Permalink
Refactor triggerErrorAndCatch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahb committed Mar 27, 2022
1 parent e7d0053 commit 309ceba
Showing 1 changed file with 23 additions and 32 deletions.
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

0 comments on commit 309ceba

Please sign in to comment.