Use UnexpectedStatusException in Zotero.HTTP.loadDocuments()

Follow-up to 76ae5d9f59, which changed loadDocuments() to pass/throw an
Error on a non-2xx response code
This commit is contained in:
Dan Stillman 2020-10-24 00:00:50 -04:00
parent 76ae5d9f59
commit 29f48476a9
2 changed files with 24 additions and 3 deletions

View file

@ -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 {

View file

@ -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);
});
});
});