From e7882869e6d4bb5e7ded893d624fc33ddda28ba9 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Mon, 25 Nov 2019 23:28:23 +0100 Subject: [PATCH] Created checkStatusAndReturnJson middleware for checking for responses status is ok(200-299) or not and if it does returns a json parsed object. --- src/api.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api.js b/src/api.js index 8668aac..ade4ceb 100644 --- a/src/api.js +++ b/src/api.js @@ -10,6 +10,13 @@ const ELASTIC_INDEX = config.ELASTIC_INDEX // TODO // - Move autorization token and errors here? +const checkStatusAndReturnJson = (response) => { + if (!response.ok) { + throw resp + } + return response.json() +} + // - - - TMDB - - - /** @@ -239,7 +246,6 @@ const login = (username, password) => { } return fetch(url.href, options) - .then(resp => checkStatus) .then(resp => resp.json()) .catch(error => { console.error('Unexpected error occured before receiving response. Error:', error)