Skip to content

Commit 1def9a7

Browse files
committed
Implcit binding + lazy evaluation instead of storing context in Raven
1 parent b6a148d commit 1def9a7

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

index.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ function createRavenMiddleware(Raven, options = {}) {
88
stateTransformer = identity,
99
breadcrumbCategory = "redux-action"
1010
} = options;
11-
Raven.setDataCallback((data, original) => {
12-
data.extra.lastAction = actionTransformer(data.extra.lastAction);
13-
data.extra.state = stateTransformer(data.extra.state);
14-
return original ? original(data) : data;
15-
});
11+
1612
return store => {
17-
// Record the initial state in case we crash before the first action
18-
// succeeds.
19-
// TODO: This does not currently work.
20-
Raven.setExtraContext({ state: store.getState() });
13+
let lastAction;
14+
15+
Raven.setDataCallback((data, original) => {
16+
data.extra.lastAction = actionTransformer(lastAction);
17+
data.extra.state = stateTransformer(store.getState());
18+
return original ? original(data) : data;
19+
});
2120

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

31-
// Set the action as context in case we crash in the reducer.
32-
const extra = { lastAction: action };
33-
const returnValue = Raven.context({ extra }, () => next(action));
34-
35-
// Set the last action and state as context in case we crash before
36-
// the next action is dispatched.
37-
Raven.setExtraContext({
38-
lastAction: action,
39-
state: store.getState()
40-
});
41-
return returnValue;
30+
lastAction = action;
31+
return next(action);
4232
};
4333
};
4434
}

index.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ const { createStore, applyMiddleware } = require("redux");
44

55
Raven.config("https://[email protected]/146969", {
66
allowDuplicates: true
7-
});
7+
}).install();
88

99
const reducer = (previousState = 0, action) => {
1010
switch (action.type) {
1111
case "THROW":
12-
throw new Error("Reducer error");
12+
// Raven does not seem to be able to capture global exceptions in Jest tests.
13+
// So we explicitly wrap this error in a Raven context.
14+
Raven.context(() => {
15+
throw new Error("Reducer error");
16+
});
1317
case "INCREMENT":
1418
return previousState + 1;
1519
case "DOUBLE":

0 commit comments

Comments
 (0)