Skip to content

Commit

Permalink
stringifying xhr error response
Browse files Browse the repository at this point in the history
  • Loading branch information
ebsi-bblake committed Sep 18, 2022
1 parent 645cf66 commit 31e6fa1
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()
})
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
})
Expand Down

0 comments on commit 31e6fa1

Please sign in to comment.