Rework OneDrive generating a 412 'Precondition Failed' Error

* Rework the 412 fix so that the OneDrive client gracefully handles the
error & retries the metadata update without the eTag/cTag causing the
issue.
* Fix the double logging to console of 'upload' and 'download' items
This commit is contained in:
abraunegg 2018-05-03 16:21:53 +10:00
parent 90dec8d6f1
commit 0b5dc66507
3 changed files with 81 additions and 59 deletions

View file

@ -21,6 +21,12 @@ void log(T...)(T args)
logfileWriteLine(args);
}
void fileOnly(T...)(T args)
{
// Write to log file only
logfileWriteLine(args);
}
void vlog(T...)(T args)
{
if (verbose) {

View file

@ -478,14 +478,14 @@ final class OneDriveApi
switch(http.statusLine.code)
{
// case 1,2,3,4:
// 200 - OK
case 200:
// No Log ..
break;
// 201 - Created OK
// 202 - Accepted
// 204 - Deleted OK
case 200,201,202,204:
case 201,202,204:
// No actions, but log if verbose logging
log.vlog("OneDrive Response: '", http.statusLine.code, " - ", http.statusLine.reason, "'");
break;
@ -520,11 +520,14 @@ final class OneDriveApi
log.vlog("OneDrive returned a 'HTTP 415 - Unsupported Media Type' - gracefully handling error");
break;
// Server side (OneDrive) Errors
// 500 - Internal Server Error
// 502 - Bad Gateway
// 503 - Service Unavailable
case 500,502,503:
// 504 - Gateway Timeout (Issue #320)
case 500,502,503,504:
// No actions
log.vlog("OneDrive returned a 'HTTP 5xx Server Side Error' - gracefully handling error");
break;
// "else"

View file

@ -404,7 +404,6 @@ final class SyncEngine
private void applyDifference(JSONValue driveItem, string driveId, bool isRoot)
{
Item item = makeItem(driveItem);
//log.vlog("Processing item to apply differences");
if (isItemRoot(driveItem) || !item.parentId || isRoot) {
item.parentId = null; // ensures that it has no parent
@ -567,7 +566,7 @@ final class SyncEngine
onedrive.downloadById(item.driveId, item.id, path);
setTimes(path, item.mtime, item.mtime);
writeln(" done.");
log.log("Downloading ", path, "... done.");
log.fileOnly("Downloading ", path, "... done.");
}
// returns true if the given item corresponds to the local one
@ -758,7 +757,7 @@ final class SyncEngine
writeln("");
response = session.upload(path, item.driveId, item.parentId, baseName(path), eTag);
}
log.log("Uploading file ", path, "... done.");
log.fileOnly("Uploading file ", path, "... done.");
// saveItem(response); redundant
// use the cTag instead of the eTag because Onedrive may update the metadata of files AFTER they have been uploaded
eTag = response["cTag"].str;
@ -950,7 +949,9 @@ final class SyncEngine
writeln("");
response = session.upload(path, parent.driveId, parent.id, baseName(path));
}
log.log("Uploading file ", path, "... done.");
log.fileOnly("Uploading file ", path, "... done.");
// Update the item's metadata on OneDrive
string id = response["id"].str;
string cTag = response["cTag"].str;
SysTime mtime = timeLastModified(path).toUTC();
@ -979,7 +980,7 @@ final class SyncEngine
writeln("");
response = session.upload(path, parent.driveId, parent.id, baseName(path));
}
log.log("Uploading file ", path, "... done.");
log.fileOnly("Uploading file ", path, "... done.");
string id = response["id"].str;
string cTag = response["cTag"].str;
SysTime mtime = timeLastModified(path).toUTC();
@ -1027,7 +1028,19 @@ final class SyncEngine
"lastModifiedDateTime": mtime.toISOExtString()
])
];
auto response = onedrive.updateById(driveId, id, data, eTag);
JSONValue response;
try {
response = onedrive.updateById(driveId, id, data, eTag);
} catch (OneDriveException e) {
if (e.httpStatusCode == 412) {
// OneDrive threw a 412 error, most likely: ETag does not match current item's value
// Retry without eTag
log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");
string nullTag = null;
response = onedrive.updateById(driveId, id, data, nullTag);
}
}
saveItem(response);
}