Skip to content

Instantly share code, notes, and snippets.

@monder
Created July 28, 2016 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monder/f3f969ca8210191b550af130627b26cf to your computer and use it in GitHub Desktop.
Save monder/f3f969ca8210191b550af130627b26cf to your computer and use it in GitHub Desktop.
react-resolve error handling
import React from 'react';
import { Resolver } from 'react-resolver';
export default function resolve(func, prop = 'error') {
return function resolveErrorDecorator(Component) {
return class ResolveError extends Resolver {
static displayName = 'ResolveError'
static propTypes = {
children: React.PropTypes.object
}
componentWillReceiveProps() {
this.setState({
pending: {},
resolved: {}
});
}
handleCatch(error) {
this.handledError = func(error);
if (this['ReactResolver.IS_CLIENT']) {
this.setState({
pending: {},
resolved: { [prop]: this.handledError }
});
}
return { id: this['ReactResolver.ID'], pending: {}, resolved: { [prop]: this.handledError } };
}
onResolve(promise) {
promise = promise
.catch((error) => {
return this.handleCatch(error);
});
return super.onResolve(promise);
}
render() {
return <Component {...this.props} {...this.state.resolved} />;
}
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment