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:
parent
76ae5d9f59
commit
29f48476a9
2 changed files with 24 additions and 3 deletions
|
@ -1208,15 +1208,22 @@ Zotero.HTTP = new function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Zotero.debug("Zotero.HTTP.loadDocuments: " + url + " loaded");
|
|
||||||
hiddenBrowser.removeEventListener("load", onLoad, true);
|
hiddenBrowser.removeEventListener("load", onLoad, true);
|
||||||
hiddenBrowser.zotero_loaded = true;
|
hiddenBrowser.zotero_loaded = true;
|
||||||
|
|
||||||
let channel = hiddenBrowser.docShell.currentDocumentChannel;
|
let channel = hiddenBrowser.docShell.currentDocumentChannel;
|
||||||
if (channel && (channel instanceof Components.interfaces.nsIHttpChannel)) {
|
if (channel && (channel instanceof Components.interfaces.nsIHttpChannel)) {
|
||||||
if (channel.responseStatus < 200 || channel.responseStatus >= 400) {
|
if (channel.responseStatus < 200 || channel.responseStatus >= 400) {
|
||||||
let e = new Error("Invalid response " + channel.responseStatus + " "
|
let response = `${channel.responseStatus} ${channel.responseStatusText}`;
|
||||||
+ channel.responseStatusText + " for '" + url + "'");
|
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) {
|
if (onError) {
|
||||||
onError(e);
|
onError(e);
|
||||||
}
|
}
|
||||||
|
@ -1227,6 +1234,8 @@ Zotero.HTTP = new function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Zotero.debug("Zotero.HTTP.loadDocuments: " + url + " loaded");
|
||||||
|
|
||||||
var maybePromise;
|
var maybePromise;
|
||||||
var error;
|
var error;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -302,5 +302,17 @@ describe("Zotero.HTTP", function () {
|
||||||
});
|
});
|
||||||
assert.isTrue(called);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue