From 058a4b1593b64ba9d142ecb20a183fdf049db746 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 16 Feb 2017 20:11:05 -0500 Subject: [PATCH] On 404 from ZFS upload, mark attachment item for upload This shouldn't happen, but reported here: https://forums.zotero.org/discussion/64386/5-0-beta-persistent-sync-errors Possibly the same cause as this: https://forums.zotero.org/discussion/64438/5-0-beta-persistent-sync-error --- chrome/content/zotero/xpcom/storage/zfs.js | 13 ++++---- test/tests/zfsTest.js | 38 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/xpcom/storage/zfs.js b/chrome/content/zotero/xpcom/storage/zfs.js index d984f08aad..98f7310fb6 100644 --- a/chrome/content/zotero/xpcom/storage/zfs.js +++ b/chrome/content/zotero/xpcom/storage/zfs.js @@ -476,13 +476,14 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = { ); throw e; } + // This shouldn't happen, but if it does, mark item for upload and restart sync else if (req.status == 404) { - Components.utils.reportError("Unexpected status code 404 in upload authorization " - + "request (" + item.libraryKey + ")"); - - // TODO: Make an API request to fix this - - throw new Error(Zotero.Sync.Storage.defaultError); + Zotero.logError(`Item ${item.libraryID}/${item.key} not found in upload authorization ` + + 'request -- marking for upload'); + yield Zotero.Sync.Data.Local.markObjectAsUnsynced(item); + return new Zotero.Sync.Storage.Result({ + syncRequired: true + }); } else if (req.status == 412) { let version = req.getResponseHeader('Last-Modified-Version'); diff --git a/test/tests/zfsTest.js b/test/tests/zfsTest.js index 97978e56a6..9221316353 100644 --- a/test/tests/zfsTest.js +++ b/test/tests/zfsTest.js @@ -132,7 +132,7 @@ describe("Zotero.Sync.Storage.Mode.ZFS", function () { assert.equal(library.storageVersion, library.libraryVersion); }) - it("should ignore a remotely missing file", function* () { + it("should ignore download for a remotely missing file", function* () { var { engine, client, caller } = yield setup(); var library = Zotero.Libraries.userLibrary; @@ -758,6 +758,42 @@ describe("Zotero.Sync.Storage.Mode.ZFS", function () { describe("#_processUploadFile()", function () { + it("should handle 404 from upload authorization request", function* () { + var { engine, client, caller } = yield setup(); + var zfs = new Zotero.Sync.Storage.Mode.ZFS({ + apiClient: client + }) + + var filePath = OS.Path.join(getTestDataDirectory().path, 'test.png'); + var item = yield Zotero.Attachments.importFromFile({ file: filePath }); + item.version = 5; + item.synced = true; + yield item.saveTx(); + + var itemJSON = item.toResponseJSON(); + itemJSON.data.mtime = yield item.attachmentModificationTime; + itemJSON.data.md5 = yield item.attachmentHash; + + server.respond(function (req) { + if (req.method == "POST" + && req.url == `${baseURL}users/1/items/${item.key}/file` + && !req.requestBody.includes('upload=')) { + req.respond( + 404, + { + "Last-Modified-Version": 5 + }, + "Not Found" + ); + } + }) + + var result = yield zfs._processUploadFile({ + name: item.libraryKey + }); + assert.isTrue(result.syncRequired); + }); + it("should handle 412 with matching version and hash matching local file", function* () { var { engine, client, caller } = yield setup(); var zfs = new Zotero.Sync.Storage.Mode.ZFS({