2021-07-07 11:04:36 +02:00
|
|
|
import { Dispatch, SetStateAction } from 'react';
|
|
|
|
|
|
|
|
import {
|
|
|
|
AuthenticationError,
|
|
|
|
ForbiddenError,
|
|
|
|
NotFoundError,
|
2022-02-11 11:19:55 +01:00
|
|
|
} from '../../../../utils/api-utils';
|
2021-07-07 11:04:36 +02:00
|
|
|
|
|
|
|
export const handleBadRequest = async (
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors: Dispatch<SetStateAction<{}>>,
|
|
|
|
res: Response,
|
|
|
|
requestId: string
|
2021-07-07 11:04:36 +02:00
|
|
|
) => {
|
2022-02-25 10:55:39 +01:00
|
|
|
const data = await res.json();
|
|
|
|
const message = data.isJoi ? data.details[0].message : data[0].msg;
|
2021-07-07 11:04:36 +02:00
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
[requestId]: message,
|
|
|
|
}));
|
2021-07-07 11:04:36 +02:00
|
|
|
|
|
|
|
throw new Error();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const handleNotFound = (
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors: Dispatch<SetStateAction<{}>>,
|
|
|
|
res: Response,
|
|
|
|
requestId: string
|
2021-07-07 11:04:36 +02:00
|
|
|
) => {
|
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
[requestId]: 'Could not find the requested resource.',
|
|
|
|
}));
|
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
throw new NotFoundError(res.status);
|
2021-07-07 11:04:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const handleUnauthorized = async (
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors: Dispatch<SetStateAction<{}>>,
|
|
|
|
res: Response,
|
|
|
|
requestId: string
|
2021-07-07 11:04:36 +02:00
|
|
|
) => {
|
2022-02-25 10:55:39 +01:00
|
|
|
const data = await res.json();
|
|
|
|
const message = data.isJoi ? data.details[0].message : data[0].msg;
|
2021-07-07 11:04:36 +02:00
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
[requestId]: message,
|
|
|
|
}));
|
2021-07-07 11:04:36 +02:00
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
throw new AuthenticationError(res.status);
|
2021-07-07 11:04:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const handleForbidden = async (
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors: Dispatch<SetStateAction<{}>>,
|
|
|
|
res: Response,
|
|
|
|
requestId: string
|
2021-07-07 11:04:36 +02:00
|
|
|
) => {
|
2022-02-25 10:55:39 +01:00
|
|
|
const data = await res.json();
|
|
|
|
const message = data.isJoi ? data.details[0].message : data[0].msg;
|
2021-10-14 19:58:57 +02:00
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
[requestId]: message,
|
|
|
|
}));
|
2021-07-07 11:04:36 +02:00
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
throw new ForbiddenError(res.status);
|
2021-07-07 11:04:36 +02:00
|
|
|
};
|