2018-04-02 14:51:02 +02:00
|
|
|
import { showAlertForError } from '../actions/alerts';
|
2016-10-18 17:09:45 +02:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
|
|
|
|
|
|
|
export default function errorsMiddleware() {
|
|
|
|
return ({ dispatch }) => next => action => {
|
2017-02-26 23:06:27 +01:00
|
|
|
if (action.type && !action.skipAlert) {
|
2016-10-18 17:09:45 +02:00
|
|
|
const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');
|
|
|
|
|
|
|
|
if (action.type.match(isFail)) {
|
2018-04-02 14:51:02 +02:00
|
|
|
dispatch(showAlertForError(action.error));
|
2016-10-18 17:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|
|
|
|
};
|