npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollbar-redux-middleware

v0.2.0

Published

Redux middleware that makes including redux state with errors sent to rollbar easier.

Downloads

4,494

Readme

rollbar-redux-middleware

Redux middleware that integrates with Rollbar. This middleware assumes the use of actions that conform to the Flux Standard Action pattern. Essentially this means that actions of the following form

{
    error: true,
    payload: new Error()
}

are considered errors to be reported to Rollbar where the error is in the payload field. We include the payload as well as the entire redux store state. Additionally, we provide a configuration option to wrap all actions in a try/catch block rather than simply using FSA style actions to denote errors.

We provide mechanisms for easily santizing the store before logging (e.g. if you store access tokens in the redux store).

Installation

Run npm install --save rollbar-redux-middleware.

Usage

Import rollbarMiddleware function from package:

import rollbarMiddleware from 'rollbar-redux-middleware';

Import rollbar and configure:

import rollbar from 'rollbar';
var Rollbar = new rollbar({accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN'});

Create middleware in your store creator:

export default function configureStore(initialState) {
  const rollbarRedux = rollbarMiddleware(Rollbar);

  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(rollbarRedux)
  );
}

Wrapping actions in a try/catch

In order to wrap actions in a try/catch block, you must pass three parameters to the function exported by this package:

import rollbarMiddleware from 'rollbar-redux-middleware';
const rollbarRedux = rollbarMiddleware(Rollbar, keyPaths, true);

The second parameter is used for state sanitization described below. If you do not need or want that functionality, simply pass an empty array, null, or the identity function as the second parameter. The third parameter of true indicates that you do want to wrap actions in a try/catch block. The first parameter is a configured Rollbar instance to use for reporting.

State sanitization

Consider the following state:

{
  user: {
    credentials: {
      token: 'ABC123',
      name: 'Bob User'
    },
    (...)
  },
  billing: {
    number: '1234 1234 1234 1234',
    (...)
  },
  (...)
}

If you want to sanitize the state before sending to Rollbar, we provide two mechanisms. The first is to include an array of keypaths that you wish to redact:

const keyPaths = [
  'billing.number',
  'user.credentials.token'
]

Then just pass this as the second parameter to rollbarMiddleware:

const rollbarRedux = rollbarMiddleware(Rollbar, keyPaths)

Alternatively, you can pass a function as the second argument to the rollbarMiddleware function. It accepts current application state and should return a state that will be sent to Rollbar.

To sanitize the example state above the same as the given keypaths, the function provided should be similar to:

const stateSanitizer = function(state) {
  // make sure you don't change state tree
  const stateCopy = Object.assign({}, state);
  // make sure you don't change billing object (by reference)
  const billingCopy = Object.assign({}, stateCopy.billing);
  // override number in billing copy
  billingCopy.number = '********';
  // pass billing copy to state copy
  stateCopy.billing = billingCopy;

  // similarly
  const userCopy = Object.assign({}, stateCopy.user);
  const credentialsCopy = Object.assign({}, userCopy.credentials)
  credentialsCopy.token = '********'
  userCopy.credentials = credentialsCopy;
  stateCopy.user = userCopy;

  // return sanitized state
  return stateCopy;
}

export default function configureStore(initialState) {
  const rollbarRedux = rollbarMiddleware(Rollbar, stateSanitizer);

  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(rollbarRedux)
  );
}