From 9f5beec80a67b47899cc3cccbfc4577e96071155 Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Thu, 7 May 2020 22:36:35 -0400 Subject: [PATCH 1/4] Fix missing error messages --- src/http.js | 56 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/http.js b/src/http.js index c3c180b1..73ae6bb2 100644 --- a/src/http.js +++ b/src/http.js @@ -11,6 +11,9 @@ import Api from './api'; import HTTP_STATUS from './http-status'; const requestPromise = require('request-promise'); +// request-promise error types +const STATUS_CODE_ERROR = 'StatusCodeError'; + /** * Isomorphic Http Promise Requests Class * @flow @@ -48,27 +51,42 @@ export default class Http { return new Promise((resolve, reject) => { const request = new window.XMLHttpRequest(); request.open(method, url); - request.onload = function() { + request.onload = function () { + let response; try { - const response = JSON.parse(request.response); - - if (request.status.toString() === HTTP_STATUS.OK) { - resolve(response); - } else { - reject( - new Error({ - body: response, - status: request.status, - }), - ); - } + response = JSON.parse(request.response); } catch (e) { - reject( - new Error({ - body: request.responseText, - status: request.status, - }), - ); + // JSON failed to parse. + reject({ + // Providing a structure compatible with the request-promise API. + // See the logic in `exceptions.js#constructErrorResponse`. + name: STATUS_CODE_ERROR, + error: { + error: { + message: 'Failed to parse response JSON.', + } + }, + statusCode: request.status, + method: request.method, + response: { + headers: request.headers + } + }); + return; + } + if (request.status.toString() === HTTP_STATUS.OK) { + resolve(response); + } else { + reject({ + // Providing a structure compatible with the request-promise API. + name: STATUS_CODE_ERROR, + error: response, + statusCode: request.status, + method: request.method, + response: { + headers: request.headers + } + }); } }; request.setRequestHeader('Content-Type', 'application/json'); From e3fd6a3eb2e4aca129138aede28f7064c286ad4d Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Thu, 7 May 2020 22:43:36 -0400 Subject: [PATCH 2/4] Small cleanups --- src/http.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/http.js b/src/http.js index 73ae6bb2..9fdc578d 100644 --- a/src/http.js +++ b/src/http.js @@ -59,18 +59,16 @@ export default class Http { // JSON failed to parse. reject({ // Providing a structure compatible with the request-promise API. - // See the logic in `exceptions.js#constructErrorResponse`. name: STATUS_CODE_ERROR, + // This double-nested "error" is because request-promise includes + // an error field in the response, which would contain the Facebook + // response JSON object, which itself contains an error field. error: { error: { message: 'Failed to parse response JSON.', } }, statusCode: request.status, - method: request.method, - response: { - headers: request.headers - } }); return; } @@ -82,10 +80,6 @@ export default class Http { name: STATUS_CODE_ERROR, error: response, statusCode: request.status, - method: request.method, - response: { - headers: request.headers - } }); } }; From f7d9bf9eac29afa9ac9e2fb9a979877ec58e87f5 Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Thu, 7 May 2020 22:57:21 -0400 Subject: [PATCH 3/4] Even more polish --- src/http.js | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/http.js b/src/http.js index 9fdc578d..d8c2e016 100644 --- a/src/http.js +++ b/src/http.js @@ -11,9 +11,6 @@ import Api from './api'; import HTTP_STATUS from './http-status'; const requestPromise = require('request-promise'); -// request-promise error types -const STATUS_CODE_ERROR = 'StatusCodeError'; - /** * Isomorphic Http Promise Requests Class * @flow @@ -56,32 +53,21 @@ export default class Http { try { response = JSON.parse(request.response); } catch (e) { - // JSON failed to parse. - reject({ - // Providing a structure compatible with the request-promise API. - name: STATUS_CODE_ERROR, - // This double-nested "error" is because request-promise includes - // an error field in the response, which would contain the Facebook - // response JSON object, which itself contains an error field. + // JSON failed to parse. Create a placeholder response. + response = { error: { - error: { - message: 'Failed to parse response JSON.', - } - }, - statusCode: request.status, - }); + message: 'Failed to parse response JSON.', + } + }; + reject(convertXhrErrorToRequestPromiseError(request, response)); return; } - if (request.status.toString() === HTTP_STATUS.OK) { - resolve(response); - } else { - reject({ - // Providing a structure compatible with the request-promise API. - name: STATUS_CODE_ERROR, - error: response, - statusCode: request.status, - }); + if (request.status.toString() !== HTTP_STATUS.OK) { + reject(convertXhrErrorToRequestPromiseError(request, response)); + return; } + + resolve(response); }; request.setRequestHeader('Content-Type', 'application/json'); request.setRequestHeader('Accept', 'application/json'); @@ -138,3 +124,18 @@ export default class Http { }); } } + +/** + * Converts the given XHR error to an error that looks like one that would + * be returned by the request-promise API. + * + * @param {XMLHttpRequest} request + * @param {any} response + */ +function convertXhrErrorToRequestPromiseError(request, response) { + return { + name: 'StatusCodeError', + error: response, + statusCode: request.status, + }; +} From f64ca56e4ea002725ad17f59d5068e3ff0faa065 Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Thu, 7 May 2020 22:57:56 -0400 Subject: [PATCH 4/4] Minor whitespace fix --- src/http.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/http.js b/src/http.js index d8c2e016..853a4484 100644 --- a/src/http.js +++ b/src/http.js @@ -128,7 +128,6 @@ export default class Http { /** * Converts the given XHR error to an error that looks like one that would * be returned by the request-promise API. - * * @param {XMLHttpRequest} request * @param {any} response */