rollbar-redux-middleware
Advanced tools
Comparing version 0.1.0 to 0.2.0
26
index.js
@@ -41,3 +41,3 @@ 'use strict'; | ||
var sanitize = exports.sanitize = function sanitize(keyPaths, state) { | ||
var sanitize = exports.sanitize = function sanitize(state, keyPaths) { | ||
if (typeof keyPaths === 'function') { | ||
@@ -58,4 +58,7 @@ return keyPaths(state); | ||
}; | ||
var stateForError = function stateForError(s, ks) { | ||
return ks ? decycle(sanitize(s, ks)) : decycle(s); | ||
}; | ||
function createMiddleware(Rollbar, keyPaths) { | ||
function createMiddleware(Rollbar, keyPaths, wrapAction) { | ||
return function (store) { | ||
@@ -65,14 +68,17 @@ return function (next) { | ||
if (!isError(action)) { | ||
if (wrapAction) { | ||
try { | ||
return next(action); | ||
} catch (err) { | ||
return Rollbar.error(err, { | ||
action: decycle(action), | ||
state: stateForError(store.getState(), keyPaths) | ||
}); | ||
} | ||
} | ||
return next(action); | ||
} | ||
var stateToSend = store.getState(); | ||
if (keyPaths) { | ||
stateToSend = sanitize(keyPaths, stateToSend); | ||
} | ||
var decycledState = decycle(stateToSend); | ||
Rollbar.error(action.payload, { | ||
state: decycledState | ||
state: stateForError(store.getState(), keyPaths) | ||
}); | ||
@@ -79,0 +85,0 @@ |
@@ -24,2 +24,56 @@ import reduxMiddleware, { | ||
describe('wrapping action', () => { | ||
let action = { | ||
type: 'I_AM_BAD', | ||
payload: 'bork' | ||
} | ||
let goodAction = { | ||
type: 'I_AM_GOOD', | ||
payload: 'bork' | ||
} | ||
let store = { | ||
getState: jest.fn(() => { | ||
return { | ||
a: 1, | ||
b: 42 | ||
} | ||
}) | ||
} | ||
let next = jest.fn((a) => { | ||
if (a.type === 'I_AM_BAD') { | ||
throw new Error(a.payload) | ||
} else { | ||
return a.payload | ||
} | ||
}) | ||
let Rollbar = { | ||
error: jest.fn() | ||
} | ||
let middleware = reduxMiddleware(Rollbar, null, true) | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
describe('when action fails', () => { | ||
test('rollbar is called', () => { | ||
middleware(store)(next)(action) | ||
expect(Rollbar.error).toHaveBeenCalled() | ||
}) | ||
test('rollbar gets the expected data', () => { | ||
middleware(store)(next)(action) | ||
expect(Rollbar.error).toBeCalledWith(new Error(action.payload), {action: JSON.stringify(action), state: '{"a":1,"b":42}'}) | ||
}) | ||
}) | ||
describe('when action does not fail', () => { | ||
test('rollbar is not called', () => { | ||
middleware(store)(next)(goodAction) | ||
expect(Rollbar.error).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
describe('with an error', () => { | ||
@@ -198,3 +252,3 @@ let action = { | ||
let state = {a: 1} | ||
let newState = sanitize(keyPaths, state) | ||
let newState = sanitize(state, keyPaths) | ||
expect(newState.a).toBe(1) | ||
@@ -206,3 +260,3 @@ expect(newState.bork).toBe(true) | ||
let state = {a: {b: 'bad', x: 'good'}, c: {d: 'bad2', y: 'good2'}, e: 'worst', f: 'bork'} | ||
let newState = sanitize(keyPaths, state) | ||
let newState = sanitize(state, keyPaths) | ||
expect(state.a.b).toBe('bad') | ||
@@ -209,0 +263,0 @@ expect(newState.a.b).toBe('********') |
{ | ||
"name": "rollbar-redux-middleware", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Redux middleware that makes including redux state with errors sent to rollbar easier.", | ||
@@ -20,3 +20,2 @@ "main": "index.js", | ||
], | ||
"author": "", | ||
"license": "MIT", | ||
@@ -23,0 +22,0 @@ "bugs": { |
@@ -14,4 +14,6 @@ # rollbar-redux-middleware | ||
are considered errors to be reported to Rollbar where the error is in the payload field. Only if there is a key of `error` with a | ||
value equal to `true` will this middleware send anything to Rollbar. We include the payload as well as the entire redux store state. | ||
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. | ||
@@ -51,2 +53,17 @@ __We provide mechanisms for easily santizing the store before logging (e.g. if you store access tokens in the redux store).__ | ||
## 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: | ||
```js | ||
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 | ||
@@ -53,0 +70,0 @@ Consider the following state: |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107685
317
142