@reduxjs/toolkit#Middleware TypeScript Examples

The following examples show how to use @reduxjs/toolkit#Middleware. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: notificationStateSlice.ts    From prism-frontend with MIT License 6 votes vote down vote up
errorToNotificationMiddleware: Middleware<{}, RootState> = () => (
  dispatch: AppDispatch,
) => (action: AnyAction) => {
  let dispatchResult;
  try {
    // catch sync errors
    // eslint-disable-next-line fp/no-mutation
    dispatchResult = dispatch(action);
  } catch (err) {
    dispatch(
      addNotification({
        type: 'error',
        message: err.message || err,
      }),
    );
    throw err;
  }

  // typical layout for rejected thunk e.g mapState/loadLayerData/rejected
  const thunkRejectedRegex = /^[A-z]+\/[A-z]+\/rejected$/;

  if (thunkRejectedRegex.test(action.type)) {
    const errorMessage = action.error.message || action.error;

    dispatch(
      addNotification({
        type: 'error',
        message: errorMessage,
      }),
    );
    console.error(`Above error(s) caused by: ${errorMessage}`);
  }

  return dispatchResult;
}
Example #2
Source File: store.ts    From webminidisc with GNU General Public License v2.0 5 votes vote down vote up
errorCatcher: Middleware = store => next => async action => {
    try {
        await next(action);
    } catch (e) {
        console.error(e);
        next(panicDialogActions.setVisible(true));
    }
}