Created
July 26, 2019 09:14
-
-
Save james-gardner/ce6a598af8eed8f8a64e830fad02344f to your computer and use it in GitHub Desktop.
Example HTTP Call
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
function getFruit() { | |
try { | |
// Axios will trip on all HTTP error codes, pushing you into the catch. | |
const res = await axios.get('http://www.example.fake/fruit'); | |
} catch (err) { | |
// Examine error for http status code. | |
if (err.status) { | |
// Give more explanation if the API provides it. | |
throw new Error(`Call failed because: $err.status`); | |
} | |
// Pass on the error or format it some more. | |
throw err; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment