Skip to content

Commit 858f613

Browse files
authored
Merge pull request #5 from willmorgan/features/add-dispatch-and-action
Provide dispatch and lastAction parameters to errorHandler
2 parents 60025a3 + bc1adcd commit 858f613

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import reduxCatch from 'redux-catch';
1010

1111
import reducer from './reducer';
1212

13-
function errorHandler(error, getState) {
13+
function errorHandler(error, getState, dispatch, lastAction) {
1414
console.error(error);
1515
console.debug('current state', getState());
16+
console.debug('last action was', lastAction);
17+
// optionally dispatch an action due to the error using the dispatch param
1618
}
1719

1820
const store = createStore(reducer, applyMiddleware(

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function middlewareFactory(errorHandler) {
55
try {
66
return next(action);
77
} catch (error) {
8-
errorHandler(error, store.getState);
8+
errorHandler(error, store.getState, store.dispatch, action);
99
return error;
1010
}
1111
};

test.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@ const mockedMiddlewareAPI = {
55
getState: function getState() {
66
return 'test';
77
},
8+
dispatch: function dispatch() {
9+
return dispatch;
10+
}
811
};
912

1013
const baseError = new Error('There was an error.');
1114

15+
const errorAction = {
16+
type: 'ERROR',
17+
};
18+
1219
function errorCase(t) {
1320
function mockedNext() {
1421
throw baseError;
1522
}
1623

17-
t.plan(3);
24+
t.plan(5);
1825

19-
function errorHandler(error, getState) {
26+
function errorHandler(error, getState, dispatch, action) {
2027
t.ok(
2128
error.message === baseError.message,
2229
'it should receive the expected error message in the `errorHandler`'
@@ -25,9 +32,17 @@ function errorCase(t) {
2532
getState() === 'test',
2633
'it should get the expected state from `getState()`'
2734
);
35+
t.ok(
36+
dispatch === mockedMiddlewareAPI.dispatch,
37+
'dispatch should be passed to the handler'
38+
);
39+
t.ok(
40+
action === errorAction,
41+
'it should pass through the action to the handler'
42+
);
2843
}
2944

30-
const error = middleware(errorHandler)(mockedMiddlewareAPI)(mockedNext)();
45+
const error = middleware(errorHandler)(mockedMiddlewareAPI)(mockedNext)(errorAction);
3146

3247
t.ok(
3348
error.message === baseError.message,

0 commit comments

Comments
 (0)