MyCrypto/common/api/utils.js
Bryan Fillmer 2616129409 Create a basic saga pattern to handle potential network errors. (#144)
* First pass at handling API failure.

* Bity saga logic upgrade.

* Reusable response.ok logic. Small optimization to array join.

* Flow fixes.

* Streamlined some error handling, moved types.
2017-08-31 09:30:46 -07:00

25 lines
531 B
JavaScript

export function checkHttpStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
let error = new Error(response.statusText);
error.response = response;
throw error;
}
}
export function parseJSON(response) {
return response.json();
}
export async function handleJSONResponse(response, errorMessage) {
if (response.ok) {
const json = await response.json();
return json;
}
if (errorMessage) {
throw new Error(errorMessage);
}
return false;
}