From 1c5cefaffdc857d17a73617fb4dc2aeaf1948460 Mon Sep 17 00:00:00 2001 From: Fletcher Hazlehurst Date: Mon, 28 Sep 2020 10:43:32 -0700 Subject: [PATCH] Fix handling of network errors for SingleFile save --- .../zotero/xpcom/utilities_internal.js | 10 ++--- test/tests/attachmentsTest.js | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index eb8f75f9b3..2359c39ebb 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -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)); } }); }); diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index bf9f0e2025..77d7a9fcab 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -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(`Test`); + } + } + ); + + 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("