2017-11-17 22:11:18 -05:00
|
|
|
import { showAlert } from 'themes/glitch/actions/alerts';
|
2016-10-18 11:09:45 -04:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
|
|
|
|
|
|
|
export default function errorsMiddleware() {
|
|
|
|
return ({ dispatch }) => next => action => {
|
2017-02-26 17:06:27 -05:00
|
|
|
if (action.type && !action.skipAlert) {
|
2016-10-18 11:09:45 -04:00
|
|
|
const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');
|
|
|
|
|
|
|
|
if (action.type.match(isFail)) {
|
|
|
|
if (action.error.response) {
|
|
|
|
const { data, status, statusText } = action.error.response;
|
|
|
|
|
|
|
|
let message = statusText;
|
|
|
|
let title = `${status}`;
|
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
message = data.error;
|
|
|
|
}
|
|
|
|
|
2016-11-20 13:39:18 -05:00
|
|
|
dispatch(showAlert(title, message));
|
2016-10-18 11:09:45 -04:00
|
|
|
} else {
|
2017-06-11 04:42:42 -04:00
|
|
|
console.error(action.error);
|
2017-01-04 21:31:45 -05:00
|
|
|
dispatch(showAlert('Oops!', 'An unexpected error occurred.'));
|
2016-10-18 11:09:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|
|
|
|
};
|