Skip to content

Commit 35326c8

Browse files
committed
Middleware can now call an error action (in much the same way as success) and also display an error message as a notification if one is sent back from the server.
1 parent 0172996 commit 35326c8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/redux/middleware/FetchMiddleware.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { notificationManager } from 'utils/NotificationManager';
2+
13
const fetchMiddleware = store => next => action => {
24
next(action);
35

@@ -23,7 +25,24 @@ const fetchMiddleware = store => next => action => {
2325
.then(function (response) {
2426
return response.json();
2527
}).then(function (json) {
26-
console.log(json);
28+
if (json.status === 'error') {
29+
if (json.message) {
30+
notificationManager.addNotification({
31+
message: json.message,
32+
level: 'error'
33+
});
34+
}
35+
if (action.meta.error) {
36+
var errorResult = action.meta.error(json, action.payload);
37+
// Check to see if the result has a type attribute, if it does then
38+
// assume its a Redux action and dispatch it.
39+
if (errorResult && errorResult.type) {
40+
next(errorResult);
41+
}
42+
}
43+
return;
44+
}
45+
2746
if (action.meta.success) {
2847
var successResult = action.meta.success(json, action.payload);
2948
// Check to see if the result has a type attribute, if it does then

0 commit comments

Comments
 (0)