From 1193b2836373d9eb76cf64364de5d2dec4020e74 Mon Sep 17 00:00:00 2001 From: davebeckman Date: Fri, 23 Mar 2018 13:59:52 -0500 Subject: [PATCH] Handle HTTP status errors (#20) --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fdb3ad5..2b5f646 100644 --- a/index.js +++ b/index.js @@ -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