1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00
unleash.unleash/frontend/src/hooks/api/getters/httpErrorResponseHandler.ts

24 lines
508 B
TypeScript

import { ResponseError } from 'utils/apiUtils';
const handleErrorResponses = (target: string) => async (res: Response) => {
if (!res.ok) {
throw new ResponseError(
target,
res.status,
await parseErrorResponse(res)
);
}
return res;
};
const parseErrorResponse = async (res: Response): Promise<unknown> => {
try {
return await res.json();
} catch {
return res.statusText;
}
};
export default handleErrorResponses;