Merge branch 'revert-530329bf' into 'develop'
Revert "Merge branch 'disable-errors-middleware' into 'develop'" See merge request soapbox-pub/soapbox!2631
This commit is contained in:
commit
889184caf5
2 changed files with 35 additions and 0 deletions
33
app/soapbox/middleware/errors.ts
Normal file
33
app/soapbox/middleware/errors.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import toast from 'soapbox/toast';
|
||||||
|
|
||||||
|
import type { AnyAction } from 'redux';
|
||||||
|
import type { ThunkMiddleware } from 'redux-thunk';
|
||||||
|
|
||||||
|
/** Whether the action is considered a failure. */
|
||||||
|
const isFailType = (type: string): boolean => type.endsWith('_FAIL');
|
||||||
|
|
||||||
|
/** Whether the action is a failure to fetch from browser storage. */
|
||||||
|
const isRememberFailType = (type: string): boolean => type.endsWith('_REMEMBER_FAIL');
|
||||||
|
|
||||||
|
/** Whether the error contains an Axios response. */
|
||||||
|
const hasResponse = (error: any): boolean => Boolean(error && error.response);
|
||||||
|
|
||||||
|
/** Don't show 401's. */
|
||||||
|
const authorized = (error: any): boolean => error?.response?.status !== 401;
|
||||||
|
|
||||||
|
/** Whether the error should be shown to the user. */
|
||||||
|
const shouldShowError = ({ type, skipAlert, error }: AnyAction): boolean => {
|
||||||
|
return !skipAlert && hasResponse(error) && authorized(error) && isFailType(type) && !isRememberFailType(type);
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Middleware to display Redux errors to the user. */
|
||||||
|
const errorsMiddleware = (): ThunkMiddleware =>
|
||||||
|
() => next => action => {
|
||||||
|
if (shouldShowError(action)) {
|
||||||
|
toast.showAlertForError(action.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(action);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default errorsMiddleware;
|
|
@ -1,6 +1,7 @@
|
||||||
import { configureStore } from '@reduxjs/toolkit';
|
import { configureStore } from '@reduxjs/toolkit';
|
||||||
import thunk, { ThunkDispatch } from 'redux-thunk';
|
import thunk, { ThunkDispatch } from 'redux-thunk';
|
||||||
|
|
||||||
|
import errorsMiddleware from './middleware/errors';
|
||||||
import soundsMiddleware from './middleware/sounds';
|
import soundsMiddleware from './middleware/sounds';
|
||||||
import appReducer from './reducers';
|
import appReducer from './reducers';
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ export const store = configureStore({
|
||||||
reducer: appReducer,
|
reducer: appReducer,
|
||||||
middleware: [
|
middleware: [
|
||||||
thunk,
|
thunk,
|
||||||
|
errorsMiddleware(),
|
||||||
soundsMiddleware(),
|
soundsMiddleware(),
|
||||||
],
|
],
|
||||||
devTools: true,
|
devTools: true,
|
||||||
|
|
Loading…
Reference in a new issue