Fix handling of network errors for SingleFile save

This commit is contained in:
Fletcher Hazlehurst 2020-09-28 10:43:32 -07:00
parent 20c8cede4d
commit 1c5cefaffd
2 changed files with 44 additions and 5 deletions

View file

@ -576,7 +576,7 @@ Zotero.Utilities.Internal = {
xhrRequest.withCredentials = true;
xhrRequest.responseType = "arraybuffer";
xhrRequest.onerror = (e) => {
let error = new Error(e.detail);
let error = e.detail;
onDone(Components.utils.cloneInto(error, sandbox));
};
xhrRequest.onreadystatechange = () => {
@ -591,7 +591,7 @@ Zotero.Utilities.Internal = {
onDone(Components.utils.cloneInto(res, sandbox));
}
else {
let error = new Error('Bad Status or Length');
let error = 'Bad Status or Length';
onDone(Components.utils.cloneInto(error, sandbox));
}
}
@ -615,13 +615,13 @@ Zotero.Utilities.Internal = {
catch (error) {
let response = await new Promise((resolve, reject) => {
coFetch(url, (response) => {
if (response.status) {
if (typeof response === 'object') {
resolve(response);
}
else {
Zotero.debug("Error retrieving url: " + url);
Zotero.debug(response.message);
reject();
Zotero.debug(response);
reject(new Error(response));
}
});
});

View file

@ -424,6 +424,45 @@ describe("Zotero.Attachments", function() {
let expectedContents = await Zotero.File.getBinaryContentsAsync(expectedPath);
assert.equal(contents, expectedContents);
});
it("should save a document with embedded files that throw errors", async function () {
var item = await createDataObject('item');
var url = "file://" + OS.Path.join(getTestDataDirectory().path, "snapshot", "foobar.gif");
httpd.registerPathHandler(
'/index.html',
{
handle: function (request, response) {
response.setStatusLine(null, 200, "OK");
response.write(`<html><head><title>Test</title></head><body><img src="${url}"/>`);
}
}
);
var deferred = Zotero.Promise.defer();
win.addEventListener('pageshow', () => deferred.resolve());
win.loadURI(testServerPath + "/index.html");
await deferred.promise;
var attachment = await Zotero.Attachments.importFromDocument({
document: win.content.document,
parentItemID: item.id
});
assert.equal(attachment.getField('url'), testServerPath + "/index.html");
// Check for embedded files
var storageDir = Zotero.Attachments.getStorageDirectory(attachment).path;
var file = await attachment.getFilePathAsync();
assert.equal(OS.Path.basename(file), 'index.html');
assert.isFalse(await OS.File.exists(OS.Path.join(storageDir, 'images', '1.gif')));
// Check attachment html file contents
let path = OS.Path.join(storageDir, 'index.html');
assert.isTrue(await OS.File.exists(path));
let contents = await Zotero.File.getContentsAsync(path);
assert.isTrue(contents.startsWith("<html><!--\n Page saved with SingleFileZ"));
});
});
describe("#importFromPageData()", function () {