From 29f48476a9cf076d2e11950d82d2045440247223 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 24 Oct 2020 00:00:50 -0400 Subject: [PATCH] Use UnexpectedStatusException in Zotero.HTTP.loadDocuments() Follow-up to 76ae5d9f597, which changed loadDocuments() to pass/throw an Error on a non-2xx response code --- chrome/content/zotero/xpcom/http.js | 15 ++++++++++++--- test/tests/httpTest.js | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js index d1b03466c0..2bbff8917f 100644 --- a/chrome/content/zotero/xpcom/http.js +++ b/chrome/content/zotero/xpcom/http.js @@ -1208,15 +1208,22 @@ Zotero.HTTP = new function() { return; } - Zotero.debug("Zotero.HTTP.loadDocuments: " + url + " loaded"); hiddenBrowser.removeEventListener("load", onLoad, true); hiddenBrowser.zotero_loaded = true; let channel = hiddenBrowser.docShell.currentDocumentChannel; if (channel && (channel instanceof Components.interfaces.nsIHttpChannel)) { if (channel.responseStatus < 200 || channel.responseStatus >= 400) { - let e = new Error("Invalid response " + channel.responseStatus + " " - + channel.responseStatusText + " for '" + url + "'"); + let response = `${channel.responseStatus} ${channel.responseStatusText}`; + Zotero.debug(`Zotero.HTTP.loadDocuments: ${url} failed with ${response}`, 2); + let e = new Zotero.HTTP.UnexpectedStatusException( + { + status: channel.responseStatus, + channel + }, + url, + `Invalid response ${response} for ${url}` + ); if (onError) { onError(e); } @@ -1227,6 +1234,8 @@ Zotero.HTTP = new function() { } } + Zotero.debug("Zotero.HTTP.loadDocuments: " + url + " loaded"); + var maybePromise; var error; try { diff --git a/test/tests/httpTest.js b/test/tests/httpTest.js index d57703370b..7dc327363b 100644 --- a/test/tests/httpTest.js +++ b/test/tests/httpTest.js @@ -302,5 +302,17 @@ describe("Zotero.HTTP", function () { }); assert.isTrue(called); }); + + it("should fail on non-2xx response", async function () { + var e = await getPromiseError(new Zotero.Promise((resolve, reject) => { + Zotero.HTTP.loadDocuments( + baseURL + "nonexistent", + () => {}, + resolve, + reject + ); + })); + assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException); + }); }); });