From 31e6fa1fc3bf231f3c26fd29cb19f98b784fb1b5 Mon Sep 17 00:00:00 2001 From: bblake Date: Sun, 18 Sep 2022 07:18:23 -0500 Subject: [PATCH] stringifying xhr error response --- request/request.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/request/request.js b/request/request.js index 7252ccade..c3a2904b5 100644 --- a/request/request.js +++ b/request/request.js @@ -3,13 +3,13 @@ var buildPathname = require("../pathname/build") var hasOwn = require("../util/hasOwn") -module.exports = function($window, oncompletion) { +module.exports = function ($window, oncompletion) { function PromiseProxy(executor) { return new Promise(executor) } function makeRequest(url, args) { - return new Promise(function(resolve, reject) { + return new Promise(function (resolve, reject) { url = buildPathname(url, args.params) var method = args.method != null ? args.method.toUpperCase() : "GET" var body = args.body @@ -20,7 +20,7 @@ module.exports = function($window, oncompletion) { var original = xhr, replacedAbort var abort = xhr.abort - xhr.abort = function() { + xhr.abort = function () { aborted = true abort.call(this) } @@ -43,7 +43,7 @@ module.exports = function($window, oncompletion) { } } - xhr.onreadystatechange = function(ev) { + xhr.onreadystatechange = function (ev) { // Don't throw errors on xhr.abort(). if (aborted) return @@ -93,9 +93,9 @@ module.exports = function($window, oncompletion) { resolve(response) } else { - var completeErrorResponse = function() { + var completeErrorResponse = function () { try { message = ev.target.responseText } - catch (e) { message = response } + catch (e) { message = JSON.stringify(response) } var error = new Error(message) error.code = ev.target.status error.response = response @@ -107,7 +107,7 @@ module.exports = function($window, oncompletion) { // This allows `xhr.ontimeout` to run in the case that there is a timeout // Without this setTimeout, `xhr.ontimeout` doesn't have a chance to reject // as `xhr.onreadystatechange` will run before it - setTimeout(function() { + setTimeout(function () { if (isTimeout) return completeErrorResponse() }) @@ -133,7 +133,7 @@ module.exports = function($window, oncompletion) { // Propagate the `abort` to any replacement XHR as well. if (xhr !== original) { replacedAbort = xhr.abort - xhr.abort = function() { + xhr.abort = function () { aborted = true replacedAbort.call(this) } @@ -161,7 +161,7 @@ module.exports = function($window, oncompletion) { } return { - request: function(url, args) { + request: function (url, args) { if (typeof url !== "string") { args = url; url = url.url } else if (args == null) args = {} var promise = makeRequest(url, args) @@ -183,10 +183,10 @@ module.exports = function($window, oncompletion) { // corresponding comment in `request/tests/test-request.js` for // a bit more background on the issue at hand. promise.constructor = PromiseProxy - promise.then = function() { + promise.then = function () { count++ var next = then.apply(promise, arguments) - next.then(complete, function(e) { + next.then(complete, function (e) { complete() if (count === 0) throw e })