Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Created July 26, 2019 09:14
Show Gist options
  • Save james-gardner/ce6a598af8eed8f8a64e830fad02344f to your computer and use it in GitHub Desktop.
Save james-gardner/ce6a598af8eed8f8a64e830fad02344f to your computer and use it in GitHub Desktop.
Example HTTP Call
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