Refactoring

This commit is contained in:
Philipp Czora
2018-11-15 08:40:13 +01:00
parent e1a0a367b4
commit 2511ba4a4a
2 changed files with 34 additions and 33 deletions

View File

@@ -11,38 +11,32 @@ const fetchOptions: RequestOptions = {
}
};
// TODO: dedup
function handleStatusCode(response: Response) {
if (!response.ok) {
if (response.status === 401) {
return response.json().then(
json => {
throw Error(json.message);
},
() => {
throw UNAUTHORIZED_ERROR;
}
);
switch (response.status) {
case 401:
return throwErrorWithMessage(response, UNAUTHORIZED_ERROR.message);
case 404:
return throwErrorWithMessage(response, NOT_FOUND_ERROR.message);
default:
return throwErrorWithMessage(response, "server returned status code " + response.status);
}
if (response.status === 404) {
return response.json().then(
json => {
throw Error(json.message);
},
() => {
throw NOT_FOUND_ERROR;
}
);
}
return response.json().then(json => {
throw Error(json.message);
}, () => {
throw new Error("server returned status code " + response.status);
});
}
return response;
}
function throwErrorWithMessage(response: Response, message: string) {
return response.json().then(
json => {
throw Error(json.message);
},
() => {
throw Error(message);
}
);
}
export function createUrl(url: string) {
if (url.includes("://")) {
return url;