Skip to content

Commit

Permalink
Handle HTTP status errors (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
davebeckman authored and RyanZim committed Mar 23, 2018
1 parent fead60f commit 1193b28
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const baseUrl = 'https://min-api.cryptocompare.com/data/'

function fetchJSON (url) {
return fetch(url)
.then(res => res.json())
.then(res => {
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`)
}
return res.json()
})
.then(body => {
if (body.Response === 'Error') throw body.Message
return body
Expand Down

0 comments on commit 1193b28

Please sign in to comment.