From 30ba85d9373832469a12c2534a46227c60c003b4 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Thu, 10 Mar 2016 12:11:45 +0100 Subject: [PATCH 1/2] Fix "Unexpected end of input" JSON parse exceptions --- js/utils/requests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/utils/requests.js b/js/utils/requests.js index 5552731a..1970548b 100644 --- a/js/utils/requests.js +++ b/js/utils/requests.js @@ -39,8 +39,8 @@ class Requests { // If the responses' body does not contain any data, // fetch will resolve responseText to the string 'None'. // If this is the case, we can not try to parse it as JSON. - if(responseText !== 'None') { - let body = JSON.parse(responseText); + if (responseText && responseText !== 'None') { + const body = JSON.parse(responseText); if(body && body.errors) { let error = new Error('Form Error'); From 8952a607b794ff5bb274e01b64d3732e21d55af0 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Thu, 10 Mar 2016 12:16:30 +0100 Subject: [PATCH 2/2] Simplify error handling in requests --- js/utils/requests.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/js/utils/requests.js b/js/utils/requests.js index 1970548b..035cf598 100644 --- a/js/utils/requests.js +++ b/js/utils/requests.js @@ -13,11 +13,11 @@ class Requests { unpackResponse(url) { return (response) => { if (response == null) { - throw new Error('For: ' + url + ' - Server did not respond to the request. (Not even displayed a 500)'); + throw new Error(`For: ${url} - Server did not respond to the request. (Not even displayed a 500)`); } if (response.status >= 500) { - let err = new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url); + let err = new Error(`${response.status} - ${response.statusText} - on URL: ${response.url}`); return response .text() @@ -25,11 +25,11 @@ class Requests { const resJson = JSON.parse(resText); err = new Error(resJson.errors.pop()); - // ES6 promises don't have a .finally() clause so - // we fake that here by forcing the .catch() clause - // to run + // ES6 promises don't have a .finally() clause so we fake that here by + // forcing the .catch() clause to run return Promise.reject(); }) + // If parsing the resText throws, just rethrow the original error we created .catch(() => { throw err; }); } @@ -42,15 +42,16 @@ class Requests { if (responseText && responseText !== 'None') { const body = JSON.parse(responseText); - if(body && body.errors) { - let error = new Error('Form Error'); + if (body && body.errors) { + const error = new Error('Form Error'); error.json = body; reject(error); - } else if(body && body.detail) { + } else if (body && body.detail) { reject(new Error(body.detail)); - } else if('success' in body && !body.success) { - let error = new Error('Client Request Error'); + } else if (body && 'success' in body && !body.success) { + const error = new Error('Client Request Error'); error.json = { + body: body, status: response.status, statusText: response.statusText, type: response.type, @@ -60,13 +61,10 @@ class Requests { } else { resolve(body); } - + } else if (response.status >= 400) { + reject(new Error(`${response.status} - ${response.statusText} - on URL: ${response.url}`)); } else { - if(response.status >= 400) { - reject(new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url)); - } else { - resolve({}); - } + resolve({}); } }).catch(reject); });