InvokeAI/invokeai/frontend/web/src/services/api/authToastMiddleware.ts
Mary Hipp Rogers b519b6e1e0
add middleware to handle 403 errors (#5245)
* add middleware to handle 403 errors

* remove log

* add logic to warn the user if not all requested images could be deleted

* lint

* fix copy

* feat(ui): simplify batchEnqueuedListener error toast logic

* feat(ui): use translations for error messages

* chore(ui): lint

---------

Co-authored-by: Mary Hipp <maryhipp@Marys-MacBook-Air.local>
Co-authored-by: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
2023-12-07 19:26:15 -05:00

27 lines
805 B
TypeScript

import { isRejectedWithValue } from '@reduxjs/toolkit';
import type { MiddlewareAPI, Middleware } from '@reduxjs/toolkit';
import { addToast } from 'features/system/store/systemSlice';
import { t } from 'i18next';
export const authToastMiddleware: Middleware =
(api: MiddlewareAPI) => (next) => (action) => {
if (isRejectedWithValue(action)) {
if (action.payload.status === 403) {
const { dispatch } = api;
const customMessage =
action.payload.data.detail !== 'Forbidden'
? action.payload.data.detail
: undefined;
dispatch(
addToast({
title: t('common.somethingWentWrong'),
status: 'error',
description: customMessage,
})
);
}
}
return next(action);
};