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

80 lines
1.8 KiB
TypeScript
Raw Normal View History

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);
};