Skip to content

Commit

Permalink
rejecting response when json
Browse files Browse the repository at this point in the history
  • Loading branch information
ebsi-bblake committed Sep 18, 2022
1 parent 6edfeb7 commit d1b4c45
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 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,21 +93,25 @@ module.exports = function($window, oncompletion) {
resolve(response)
}
else {
var completeErrorResponse = function() {
try { message = ev.target.responseText }
catch (e) { message = JSON.stringify(response) }
var error = new Error(message)
error.code = ev.target.status
error.response = response
reject(error)
var completeErrorResponse = function () {
if (ev.target.responseType == "json") {
reject(response)
} else {
try { message = ev.target.responseText }
catch (e) { message = response }
var error = new Error(message)
error.code = ev.target.status
error.response = response
reject(error)
}
}

if (xhr.status === 0) {
// Use setTimeout to push this code block onto the event queue
// 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 +137,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 +165,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 +187,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 d1b4c45

Please sign in to comment.