Skip to content

Instantly share code, notes, and snippets.

@tcodes0
Created September 8, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcodes0/ea1d14580d6c326a096c31f3162f4590 to your computer and use it in GitHub Desktop.
Save tcodes0/ea1d14580d6c326a096c31f3162f4590 to your computer and use it in GitHub Desktop.
fetch-error-treatment
const fetchPayloadFrom = async response => {
if (response.status >= 200 && response.status < 300) {
const json = await response.json();
return json;
} else {
const bodyResp = await response.text();
const error = new Error(bodyResp);
error.status = response.status;
error.statusText = response.statusText;
throw error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment