From 053599993bcc84f606d0a8cd4fe5713e831b537d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 21 Oct 2018 06:01:06 -0400 Subject: [PATCH] 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.) --- .../zotero/xpcom/utilities_translate.js | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_translate.js b/chrome/content/zotero/xpcom/utilities_translate.js index bc49373b3c..577bfa87c9 100644 --- a/chrome/content/zotero/xpcom/utilities_translate.js +++ b/chrome/content/zotero/xpcom/utilities_translate.js @@ -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; }