This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2017-12-03 23:26:40 -08:00
|
|
|
import { showAlert } from 'flavours/glitch/actions/alerts';
|
2016-10-18 08:09:45 -07:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
|
|
|
|
|
|
|
export default function errorsMiddleware() {
|
|
|
|
return ({ dispatch }) => next => action => {
|
2017-02-26 14:06:27 -08:00
|
|
|
if (action.type && !action.skipAlert) {
|
2016-10-18 08:09:45 -07: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 10:39:18 -08:00
|
|
|
dispatch(showAlert(title, message));
|
2016-10-18 08:09:45 -07:00
|
|
|
} else {
|
2017-06-11 01:42:42 -07:00
|
|
|
console.error(action.error);
|
2017-01-04 18:31:45 -08:00
|
|
|
dispatch(showAlert('Oops!', 'An unexpected error occurred.'));
|
2016-10-18 08:09:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|
|
|
|
};
|