Error catcher middleware for mobx state tree.
Error catcher middleware for mobx state tree.
Created with reference to redux-catch
$ npm i mobx-state-tree-action-catch
# or
$ yarn add mobx-state-tree-action-catch
import actionCatch from 'mobx-state-tree-action-catch';
function errorHandler(error, snapshot: ReturnType<typeof getSnapshot>, lastAction: string) {
console.error(error);
console.error(snapshot)
console.error(lastAction)
}
const Store = types.model({
UIStore: types.optional(UIStore, {}),
})
export interface IStore extends Instance<typeof Store> {}
const createStore = (snapshot = null): IStore => {
MobxStore = Store.create()
if (snapshot) {
applySnapshot(MobxStore, snapshot)
}
// Add actionCatch to middleware
addMiddleware(MobxStore, actionCatch(errorHandler))
return MobxStore
}
export default createStore
To use it with Sentry just download the Sentry script from npm:
npm i -S raven-js raven
Now load and configure your client:
import Raven from 'raven-js';
const sentryKey = '<key>';
Raven
.config(`https://${sentryKey}@app.getsentry.com/<project>`)
.install();
And then use Raven.captureException as the error handler like this:
const createStore = (snapshot = null): IStore => {
MobxStore = Store.create()
if (snapshot) {
applySnapshot(MobxStore, snapshot)
}
// Add actionCatch to middleware
addMiddleware(MobxStore, actionCatch((error) => Raven.captureException(error), MobxStore))
return MobxStore
}