1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/api/actions/useAdminUsersApi/errorHandlers.ts
olav 878f892c50 refactor: fix misc test feedback (#709)
* refactor: keep feature toggle name when changing project

* refactor: add missing permission button tooltip

* refactor: add success toast on toggle revival

* refactor: add success toast on stale toggle

* refactor: fix initial user role checkbox value

* refactor: remove duplicated error message

* refactor: fix change-password error parsing

* refactor: remove inaccurate edit toggle toast text

* refactor: truncate long names in project cards

* refactor: truncate long project name in title

* refactor: add ellipses to truncated strings

* refactor: swap truncateString with StringTruncator

* refactor: remove unnecessary truncation

* refactor: mark context fields as optional

* refactor: show all errors from tag type creation

* refactor: show all errors from strategy create/update

* refactor: filter out empty strategies on create/update

* refactor: add an edit button to the addons list

* refactor: add missing labels

* refactor: catch errors from toggling stale features
2022-02-15 10:57:57 +01:00

80 lines
1.8 KiB
TypeScript

import { Dispatch, SetStateAction } from 'react';
import {
AuthenticationError,
ForbiddenError,
NotFoundError,
} from '../../../../utils/api-utils';
export const handleBadRequest = async (
setErrors?: Dispatch<SetStateAction<{}>>,
res?: Response,
requestId?: string
) => {
if (!setErrors || !requestId) return;
if (res) {
const data = await res.json();
const message = data.isJoi ? data.details[0].message : data[0].msg;
setErrors(prev => ({
...prev,
[requestId]: message,
}));
}
throw new Error();
};
export const handleNotFound = (
setErrors?: Dispatch<SetStateAction<{}>>,
res?: Response,
requestId?: string
) => {
if (!setErrors || !requestId) return;
setErrors(prev => ({
...prev,
[requestId]: 'Could not find the requested resource.',
}));
throw new NotFoundError(res?.status);
};
export const handleUnauthorized = async (
setErrors?: Dispatch<SetStateAction<{}>>,
res?: Response,
requestId?: string
) => {
if (!setErrors || !requestId) return;
if (res) {
const data = await res.json();
const message = data.isJoi ? data.details[0].message : data[0].msg;
setErrors(prev => ({
...prev,
[requestId]: message,
}));
}
throw new AuthenticationError(res?.status);
};
export const handleForbidden = async (
setErrors?: Dispatch<SetStateAction<{}>>,
res?: Response,
requestId?: string
) => {
if (!setErrors || !requestId) return;
if (res) {
const data = await res.json();
const message = data.isJoi ? data.details[0].message : data[0].msg;
setErrors(prev => ({
...prev,
[requestId]: message,
}));
}
throw new ForbiddenError(res?.status);
};