File tree Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -10,9 +10,11 @@ import reduxCatch from 'redux-catch';
10
10
11
11
import reducer from ' ./reducer' ;
12
12
13
- function errorHandler (error , getState ) {
13
+ function errorHandler (error , getState , dispatch , lastAction ) {
14
14
console .error (error);
15
15
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
16
18
}
17
19
18
20
const store = createStore (reducer, applyMiddleware (
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ function middlewareFactory(errorHandler) {
5
5
try {
6
6
return next ( action ) ;
7
7
} catch ( error ) {
8
- errorHandler ( error , store . getState ) ;
8
+ errorHandler ( error , store . getState , store . dispatch , action ) ;
9
9
return error ;
10
10
}
11
11
} ;
Original file line number Diff line number Diff line change @@ -5,18 +5,25 @@ const mockedMiddlewareAPI = {
5
5
getState : function getState ( ) {
6
6
return 'test' ;
7
7
} ,
8
+ dispatch : function dispatch ( ) {
9
+ return dispatch ;
10
+ }
8
11
} ;
9
12
10
13
const baseError = new Error ( 'There was an error.' ) ;
11
14
15
+ const errorAction = {
16
+ type : 'ERROR' ,
17
+ } ;
18
+
12
19
function errorCase ( t ) {
13
20
function mockedNext ( ) {
14
21
throw baseError ;
15
22
}
16
23
17
- t . plan ( 3 ) ;
24
+ t . plan ( 5 ) ;
18
25
19
- function errorHandler ( error , getState ) {
26
+ function errorHandler ( error , getState , dispatch , action ) {
20
27
t . ok (
21
28
error . message === baseError . message ,
22
29
'it should receive the expected error message in the `errorHandler`'
@@ -25,9 +32,17 @@ function errorCase(t) {
25
32
getState ( ) === 'test' ,
26
33
'it should get the expected state from `getState()`'
27
34
) ;
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
+ ) ;
28
43
}
29
44
30
- const error = middleware ( errorHandler ) ( mockedMiddlewareAPI ) ( mockedNext ) ( ) ;
45
+ const error = middleware ( errorHandler ) ( mockedMiddlewareAPI ) ( mockedNext ) ( errorAction ) ;
31
46
32
47
t . ok (
33
48
error . message === baseError . message ,
You can’t perform that action at this time.
0 commit comments