From 90971ef39dd61adadc8599fdb6b635f65476b3de Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Thu, 22 Jun 2017 11:14:41 -0700 Subject: [PATCH] Add basic error handling --- src/connect.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/connect.js b/src/connect.js index bb86f47..6e36574 100644 --- a/src/connect.js +++ b/src/connect.js @@ -47,6 +47,7 @@ export default (mapFirebaseToProps = defaultMapFirebaseToProps, mergeProps = def this.ref = path => this.firebaseApp.database().ref(path) this.state = { subscriptionsState: null, + errors: {}, } } @@ -99,11 +100,24 @@ export default (mapFirebaseToProps = defaultMapFirebaseToProps, mergeProps = def ...prevState.subscriptionsState, [key]: value, }, + errors: { + ...prevState.errors, + [key]: null, + }, })) } } + const onError = err => { + if (!this.mounted) return + this.setState(prevState => ({ + errors: { + ...prevState.errors, + [key]: err, + }, + })) + } - subscriptionRef.on('value', update) + subscriptionRef.on('value', update, onError) return { path, @@ -138,10 +152,14 @@ export default (mapFirebaseToProps = defaultMapFirebaseToProps, mergeProps = def const firebaseProps = mapFirebase(this.props, this.ref, this.firebaseApp) const actionProps = pickBy(firebaseProps, prop => typeof prop === 'function') const subscriptionProps = this.state.subscriptionsState - const props = mergeProps(this.props, { - ...actionProps, - ...subscriptionProps, - }) + const props = mergeProps( + this.props, + { + ...actionProps, + ...subscriptionProps, + }, + this.state.errors + ) return createElement(WrappedComponent, props) }