Created
September 8, 2019 17:59
-
-
Save tcodes0/ea1d14580d6c326a096c31f3162f4590 to your computer and use it in GitHub Desktop.
fetch-error-treatment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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