项目作者: PlatziDev

项目描述 :
Error catcher middleware for Redux reducers and sync middlewares
高级语言: JavaScript
项目地址: git://github.com/PlatziDev/redux-catch.git
创建时间: 2016-04-13T05:41:27Z
项目社区:https://github.com/PlatziDev/redux-catch

开源协议:MIT License

下载


redux-catch

Error catcher middleware for Redux reducers and sync middlewares.

Build Status
XO code style

API

Apply middleware

  1. import { createStore, applyMiddleware } from 'redux';
  2. import reduxCatch from 'redux-catch';
  3. import reducer from './reducer';
  4. function errorHandler(error, getState, lastAction, dispatch) {
  5. console.error(error);
  6. console.debug('current state', getState());
  7. console.debug('last action was', lastAction);
  8. // optionally dispatch an action due to the error using the dispatch parameter
  9. }
  10. const store = createStore(reducer, applyMiddleware(
  11. reduxCatch(errorHandler)
  12. ));
  • reduxCatch 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:

  1. 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:

  1. import Raven from 'raven-js';
  2. const sentryKey = '<key>';
  3. Raven
  4. .config(`https://${sentryKey}@app.getsentry.com/<project>`)
  5. .install();

And then use Raven.captureException as the error handler like this:

  1. const store = createStore(reducer, applyMiddleware(
  2. reduxCatch(error => Raven.captureException(error));
  3. ));

Now redux-catch will start to send the errors of your reducers and middlewares to Sentry.

Add state as extra data

You can also add the state data as extra data for your errors so you can know the state at the moment of the error.

  1. function errorHandler(error, getState, action) {
  2. Raven.context({
  3. state: getState(),
  4. action,
  5. });
  6. Raven.captureException(error);
  7. }