Generalize Zotero.HTTP.promise() for other methods
Also make success codes configurable with an options parameter
This commit is contained in:
parent
2e81f087b9
commit
4c8431ca7d
|
@ -7,10 +7,10 @@ Zotero.HTTP = new function() {
|
||||||
* Exception returned for unexpected status when promise* is used
|
* Exception returned for unexpected status when promise* is used
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
this.UnexpectedStatusException = function(xmlhttp) {
|
this.UnexpectedStatusException = function(xmlhttp, msg) {
|
||||||
this.xmlhttp = xmlhttp;
|
this.xmlhttp = xmlhttp;
|
||||||
this.status = xmlhttp.status;
|
this.status = xmlhttp.status;
|
||||||
this.message = "XMLHttpRequest received unexpected status "+this.status;
|
this.message = msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.UnexpectedStatusException.prototype.toString = function() {
|
this.UnexpectedStatusException.prototype.toString = function() {
|
||||||
|
@ -41,12 +41,13 @@ Zotero.HTTP = new function() {
|
||||||
* <li>cookieSandbox - The sandbox from which cookies should be taken</li>
|
* <li>cookieSandbox - The sandbox from which cookies should be taken</li>
|
||||||
* <li>dontCache - If set, specifies that the request should not be fulfilled
|
* <li>dontCache - If set, specifies that the request should not be fulfilled
|
||||||
* from the cache</li>
|
* from the cache</li>
|
||||||
|
* <li>successCodes - HTTP status codes that are considered successful</li>
|
||||||
* <li>debug - Log response text and status code</li>
|
* <li>debug - Log response text and status code</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
|
* @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
|
||||||
* @return {Promise} A promise resolved with the XMLHttpRequest object if the request
|
* @return {Promise} A promise resolved with the XMLHttpRequest object if the request
|
||||||
* succeeds, or rejected if the browser is offline or a non-2XX status response
|
* succeeds, or rejected if the browser is offline or a non-2XX status response
|
||||||
* code is received.
|
* code is received (or a code not in options.successCodes if provided).
|
||||||
*/
|
*/
|
||||||
this.promise = function promise(method, url, options) {
|
this.promise = function promise(method, url, options) {
|
||||||
if (url instanceof Components.interfaces.nsIURI) {
|
if (url instanceof Components.interfaces.nsIURI) {
|
||||||
|
@ -73,12 +74,13 @@ Zotero.HTTP = new function() {
|
||||||
bodyStart + '... (' + options.body.length + ' chars)' : bodyStart)
|
bodyStart + '... (' + options.body.length + ' chars)' : bodyStart)
|
||||||
+ " to " + dispURL);
|
+ " to " + dispURL);
|
||||||
} else {
|
} else {
|
||||||
Zotero.debug("HTTP GET " + dispURL);
|
Zotero.debug("HTTP " + method + " " + dispURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.browserIsOffline()) {
|
if (this.browserIsOffline()) {
|
||||||
return Q.fcall(function() {
|
return Q.fcall(function() {
|
||||||
Zotero.debug("HTTP GET " + dispURL + " failed: Browser is offline");
|
Zotero.debug("HTTP " + method + " " + dispURL + " failed: "
|
||||||
|
+ "Browser is offline");
|
||||||
throw new this.BrowserOfflineException();
|
throw new this.BrowserOfflineException();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -89,6 +91,13 @@ Zotero.HTTP = new function() {
|
||||||
xmlhttp.mozBackgroundRequest = true;
|
xmlhttp.mozBackgroundRequest = true;
|
||||||
xmlhttp.open(method, url, true);
|
xmlhttp.open(method, url, true);
|
||||||
|
|
||||||
|
if (method == 'PUT') {
|
||||||
|
// Some servers (e.g., Jungle Disk DAV) return a 200 response code
|
||||||
|
// with Content-Length: 0, which triggers a "no element found" error
|
||||||
|
// in Firefox, so we override to text
|
||||||
|
xmlhttp.overrideMimeType("text/plain");
|
||||||
|
}
|
||||||
|
|
||||||
// Send cookie even if "Allow third-party cookies" is disabled (>=Fx3.6 only)
|
// Send cookie even if "Allow third-party cookies" is disabled (>=Fx3.6 only)
|
||||||
var channel = xmlhttp.channel;
|
var channel = xmlhttp.channel;
|
||||||
channel.QueryInterface(Components.interfaces.nsIHttpChannelInternal);
|
channel.QueryInterface(Components.interfaces.nsIHttpChannelInternal);
|
||||||
|
@ -117,15 +126,29 @@ Zotero.HTTP = new function() {
|
||||||
|
|
||||||
xmlhttp.onloadend = function() {
|
xmlhttp.onloadend = function() {
|
||||||
var status = xmlhttp.status;
|
var status = xmlhttp.status;
|
||||||
if(options && options.debug && xmlhttp.responseText) {
|
|
||||||
Zotero.debug(xmlhttp.responseText);
|
if (options && options.successCodes) {
|
||||||
|
var success = options.successCodes.indexOf(status) != -1;
|
||||||
}
|
}
|
||||||
if(status >= 200 && status < 300) {
|
else {
|
||||||
Zotero.debug("HTTP GET " + dispURL + " succeeded (" + xmlhttp.status + ")");
|
var success = status >= 200 && status < 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(success) {
|
||||||
|
if (options && options.debug) {
|
||||||
|
Zotero.debug("HTTP " + method + " " + dispURL
|
||||||
|
+ " succeeded with " + xmlhttp.status);
|
||||||
|
Zotero.debug(xmlhttp.responseText);
|
||||||
|
}
|
||||||
deferred.resolve(xmlhttp);
|
deferred.resolve(xmlhttp);
|
||||||
} else {
|
} else {
|
||||||
Zotero.debug("HTTP GET " + dispURL + " failed: Unexpected status code " + xmlhttp.status);
|
var msg = "HTTP " + method + " " + dispURL + " failed: "
|
||||||
deferred.reject(new Zotero.HTTP.UnexpectedStatusException(xmlhttp));
|
+ "Unexpected status code " + xmlhttp.status;
|
||||||
|
Zotero.debug(msg, 1);
|
||||||
|
if (options && options.debug) {
|
||||||
|
Zotero.debug(xmlhttp.responseText);
|
||||||
|
}
|
||||||
|
deferred.reject(new Zotero.HTTP.UnexpectedStatusException(xmlhttp, msg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user