2023-05-10 03:59:29 -07:00
|
|
|
import type { AnyAction, Middleware } from 'redux';
|
|
|
|
|
2019-02-10 12:28:29 -08:00
|
|
|
import { showAlertForError } from 'flavours/glitch/actions/alerts';
|
2023-05-10 03:59:29 -07:00
|
|
|
|
|
|
|
import type { RootState } from '..';
|
2016-10-18 08:09:45 -07:00
|
|
|
|
|
|
|
const defaultFailSuffix = 'FAIL';
|
|
|
|
|
2023-05-09 07:56:26 -07:00
|
|
|
export const errorsMiddleware: Middleware<Record<string, never>, RootState> =
|
2023-05-09 14:41:18 -07:00
|
|
|
({ dispatch }) =>
|
|
|
|
(next) =>
|
2023-05-10 03:59:29 -07:00
|
|
|
(action: AnyAction & { skipAlert?: boolean; skipNotFound?: boolean }) => {
|
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');
|
|
|
|
|
2023-05-10 03:59:29 -07:00
|
|
|
if (typeof action.type === 'string' && action.type.match(isFail)) {
|
2020-03-28 09:59:45 -07:00
|
|
|
dispatch(showAlertForError(action.error, action.skipNotFound));
|
2016-10-18 08:09:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|