Add version file (#1087)

This commit is contained in:
Fábio Rosado
2023-01-03 17:31:13 +00:00
committed by GitHub
parent dbdcd0b3d0
commit 412da2de08
6 changed files with 48 additions and 63 deletions

View File

@@ -1,48 +1,24 @@
import { FetchError, ErrorCode } from "./exceptions";
import { FetchError, ErrorCode } from './exceptions';
/*
This is a fetch wrapper that handles any non 200 response and throws a FetchError
with the right ErrorCode.
TODO: Should we only throw on 4xx and 5xx responses?
*/
export async function robustFetch(url: string, options?: RequestInit): Promise<Response> {
const response = await fetch(url, options);
// Note that response.ok is true for 200-299 responses
if (!response.ok) {
const errorMsg = `Fetching from URL ${url} failed with error ${response.status} (${response.statusText}).`;
switch(response.status) {
switch (response.status) {
case 404:
throw new FetchError(
ErrorCode.FETCH_NOT_FOUND_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_NOT_FOUND_ERROR, errorMsg);
case 401:
throw new FetchError(
ErrorCode.FETCH_UNAUTHORIZED_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_UNAUTHORIZED_ERROR, errorMsg);
case 403:
throw new FetchError(
ErrorCode.FETCH_FORBIDDEN_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_FORBIDDEN_ERROR, errorMsg);
case 500:
throw new FetchError(
ErrorCode.FETCH_SERVER_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_SERVER_ERROR, errorMsg);
case 503:
throw new FetchError(
ErrorCode.FETCH_UNAVAILABLE_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_UNAVAILABLE_ERROR, errorMsg);
default:
throw new FetchError(
ErrorCode.FETCH_ERROR,
errorMsg
);
throw new FetchError(ErrorCode.FETCH_ERROR, errorMsg);
}
}
return response
return response;
}