Add successCodes argument to translator doGet/doPost
In 2652fac24
we started failing the translator if doGet/doPost returned
a >=400 status code, but at least one translator (Primo) relied on doGet
continuing after a 404. This allows translators to specify the status
codes that are allowed, similar to the same argument in
Zotero.HTTP.request().
(We'll clean up the signature at some point or just get rid of these
functions, but this is a quick fix for Primo.)
This commit is contained in:
parent
a0ca67d879
commit
053599993b
1 changed files with 24 additions and 4 deletions
|
@ -308,9 +308,11 @@ Zotero.Utilities.Translate.prototype.processDocuments = async function (urls, pr
|
|||
* @param {Function} done Callback to be executed after all documents have been loaded
|
||||
* @param {String} responseCharset Character set to force on the response
|
||||
* @param {Object} requestHeaders HTTP headers to include with request
|
||||
* @param {Number[]} successCodes - HTTP status codes that are considered
|
||||
* successful, or FALSE to allow all
|
||||
* @return {Boolean} True if the request was sent, or false if the browser is offline
|
||||
*/
|
||||
Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, responseCharset, requestHeaders) {
|
||||
Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, responseCharset, requestHeaders, successCodes) {
|
||||
var callAgain = false,
|
||||
me = this,
|
||||
translate = this._translate;
|
||||
|
@ -326,7 +328,16 @@ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, res
|
|||
|
||||
translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doGet");
|
||||
var xmlhttp = Zotero.HTTP.doGet(url, function(xmlhttp) {
|
||||
if (xmlhttp.status >= 400 || !xmlhttp.status) {
|
||||
if (successCodes) {
|
||||
var success = successCodes.includes(xmlhttp.status);
|
||||
}
|
||||
else if (successCodes === false) {
|
||||
var success = true;
|
||||
}
|
||||
else {
|
||||
var success = xmlhttp.status >= 200 && xmlhttp.status < 400;
|
||||
}
|
||||
if (!success) {
|
||||
translate.complete(false, `HTTP GET ${url} failed with status code ${xmlhttp.status}`);
|
||||
return;
|
||||
}
|
||||
|
@ -354,13 +365,22 @@ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, res
|
|||
* Already documented in Zotero.HTTP
|
||||
* @ignore
|
||||
*/
|
||||
Zotero.Utilities.Translate.prototype.doPost = function(url, body, onDone, headers, responseCharset) {
|
||||
Zotero.Utilities.Translate.prototype.doPost = function(url, body, onDone, headers, responseCharset, successCodes) {
|
||||
var translate = this._translate;
|
||||
url = translate.resolveURL(url);
|
||||
|
||||
translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doPost");
|
||||
var xmlhttp = Zotero.HTTP.doPost(url, body, function(xmlhttp) {
|
||||
if (xmlhttp.status >= 400 || !xmlhttp.status) {
|
||||
if (successCodes) {
|
||||
var success = successCodes.includes(xmlhttp.status);
|
||||
}
|
||||
else if (successCodes === false) {
|
||||
var success = true;
|
||||
}
|
||||
else {
|
||||
var success = xmlhttp.status >= 200 && xmlhttp.status < 400;
|
||||
}
|
||||
if (!success) {
|
||||
translate.complete(false, `HTTP POST ${url} failed with status code ${xmlhttp.status}`);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue