generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: errorboundary for PluginContainer
- Loading branch information
1 parent
60d367d
commit d57e022
Showing
5 changed files
with
118 additions
and
41 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
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
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,49 @@ | ||
/** FOR TESTING PURPOSES ONLY */ | ||
/** This mock is used to mock the ErrorBoundary component to avoid having to deal with <ErrorPage /> in testing */ | ||
|
||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
/** | ||
* Error boundary component used to log caught errors and display the error page. | ||
* | ||
* @memberof module:React | ||
* @extends {Component} | ||
*/ | ||
class MockErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
// Update state so the next render will show the fallback UI. | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
logError(error, { stack: info.componentStack }); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
// Render "Try again" instead of <Error Page /> from frontend-platform | ||
return this.props.fallbackComponent || <div>Try again</div>; | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
MockErrorBoundary.propTypes = { | ||
children: PropTypes.node, | ||
fallbackComponent: PropTypes.node, | ||
}; | ||
|
||
MockErrorBoundary.defaultProps = { | ||
children: null, | ||
fallbackComponent: undefined, | ||
}; | ||
|
||
export default MockErrorBoundary; |