Make Hyper more robust against plugins

* Add some try/catch
* Support React@16 error boundaries for render decorated components
This commit is contained in:
CHaBou 2017-09-13 22:55:54 +02:00
parent 3e632577e5
commit 9bd410f1e4
No known key found for this signature in database
GPG key ID: EF8D073B729A0B33
2 changed files with 51 additions and 8 deletions

View file

@ -431,7 +431,11 @@ function exposeDecorated(Component_) {
}
onRef(decorated_) {
if (this.props.onDecorated) {
this.props.onDecorated(decorated_);
try {
this.props.onDecorated(decorated_);
} catch (e) {
notify('Plugin error', `Error occurred. Check Developer Tools for details`);
}
}
}
render() {
@ -487,9 +491,21 @@ function getDecorated(parent, name) {
// that wraps with the higher-order components
// exposed by plugins
export function decorate(Component_, name) {
return class DecoratedCompenent extends React.Component {
return class DecoratedComponent extends React.Component {
constructor(props) {
super(props);
this.state = {has_error: false};
}
componentDidCatch() {
this.setState({hasError: true});
// No need to detail this error because React print those informations.
notify(
'Plugin error',
`Plugins decorating ${name} has been disabled because of a plugin crash. Check Developer Tools for details.`
);
}
render() {
const Sub = getDecorated(Component_, name);
const Sub = this.state.hasError ? Component_ : getDecorated(Component_, name);
return React.createElement(Sub, this.props);
}
};