diff --git a/request/request.js b/request/request.js index 94053650e..0c317b866 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,13 +93,17 @@ 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) { @@ -107,7 +111,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 +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) } @@ -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) @@ -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 })