Skip to content

Commit

Permalink
Implcit binding + lazy evaluation instead of storing context in Raven
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Apr 21, 2017
1 parent b6a148d commit 1def9a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
30 changes: 10 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ function createRavenMiddleware(Raven, options = {}) {
stateTransformer = identity,
breadcrumbCategory = "redux-action"
} = options;
Raven.setDataCallback((data, original) => {
data.extra.lastAction = actionTransformer(data.extra.lastAction);
data.extra.state = stateTransformer(data.extra.state);
return original ? original(data) : data;
});

return store => {
// Record the initial state in case we crash before the first action
// succeeds.
// TODO: This does not currently work.
Raven.setExtraContext({ state: store.getState() });
let lastAction;

Raven.setDataCallback((data, original) => {
data.extra.lastAction = actionTransformer(lastAction);
data.extra.state = stateTransformer(store.getState());
return original ? original(data) : data;
});

return next => action => {
// Log the action taken to Raven so that we have narrative context in our
Expand All @@ -28,17 +27,8 @@ function createRavenMiddleware(Raven, options = {}) {
data: breadcrumbDataFromAction(action)
});

// Set the action as context in case we crash in the reducer.
const extra = { lastAction: action };
const returnValue = Raven.context({ extra }, () => next(action));

// Set the last action and state as context in case we crash before
// the next action is dispatched.
Raven.setExtraContext({
lastAction: action,
state: store.getState()
});
return returnValue;
lastAction = action;
return next(action);
};
};
}
Expand Down
8 changes: 6 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ const { createStore, applyMiddleware } = require("redux");

Raven.config("https://5d5bf17b1bed4afc9103b5a09634775e@sentry.io/146969", {
allowDuplicates: true
});
}).install();

const reducer = (previousState = 0, action) => {
switch (action.type) {
case "THROW":
throw new Error("Reducer error");
// Raven does not seem to be able to capture global exceptions in Jest tests.
// So we explicitly wrap this error in a Raven context.
Raven.context(() => {
throw new Error("Reducer error");
});
case "INCREMENT":
return previousState + 1;
case "DOUBLE":
Expand Down

0 comments on commit 1def9a7

Please sign in to comment.