Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-state-tree-action-catch

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-state-tree-action-catch

Error catcher middleware for mobx state tree

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

mobx-state-tree-action-catch

Error catcher middleware for mobx state tree.

Created with reference to redux-catch

Installing

$ npm i mobx-state-tree-action-catch
# or
$ yarn add mobx-state-tree-action-catch

How to use

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
  • actionCatch receive a function to use when an error happen.
  • The error handler function could be just a console.error like the example above or a function to log the error in some kind of error tracking platform.
  • You should use this middleware as the first middleware in the chain, so its allowed to catch all the possible errors in the application.

Using it with Sentry

To use it with Sentry just download the Sentry script from npm:

npm i -S raven-js raven
  • raven-js: This is the client for browser usage.
  • raven-node: This is the client for server usage.

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
}

FAQs

Package last updated on 22 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc