Update 'HTTP 412 - Precondition Failed' error handling (#67)

* Update 'HTTP 412 - Precondition Failed' error handling based on local testing to ensure a http 412 response is gracefully handled & the modified file is subsequently uploaded to OneDrive successfully.
This commit is contained in:
abraunegg 2018-07-16 06:36:31 +10:00 committed by GitHub
parent 54ae6eacad
commit a4e055fdc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 11 deletions

View file

@ -540,8 +540,12 @@ final class OneDriveApi
private void checkHttpCode(ref const JSONValue response)
{
if (http.statusLine.code / 100 != 2) {
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
if (http.statusLine.code == 412) {
throw new OneDriveException(http.statusLine.code, http.statusLine.reason);
} else {
if (http.statusLine.code / 100 != 2) {
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
}
}
}
}

View file

@ -824,7 +824,7 @@ final class SyncEngine
string eTag = item.eTag;
if (!testFileHash(path, item)) {
log.vlog("The file content has changed");
write("Uploading file ", path, " ...");
write("Uploading file ", path, " ... ");
JSONValue response;
// Are we using OneDrive Personal or OneDrive Business?
@ -846,20 +846,28 @@ final class SyncEngine
log.fileOnly(path, " is currently checked out or locked for editing by another user.");
return;
}
if (e.httpStatusCode == 412) {
// HTTP request returned status code 412 - ETag does not match current item's value
// Remove the offending file from OneDrive - file will be uploaded as a new file
onedrive.deleteById(item.driveId, item.id, item.eTag);
// Delete from the local database
itemdb.deleteById(item.driveId, item.id);
}
if (e.httpStatusCode == 504) {
// HTTP request returned status code 504 (Gateway Timeout)
// Try upload as a session
response = session.upload(path, item.driveId, item.parentId, baseName(path), eTag);
response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag);
}
else throw e;
}
writeln(" done.");
writeln("done.");
} else {
writeln("");
response = session.upload(path, item.driveId, item.parentId, baseName(path), eTag);
writeln(" done.");
}
response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag);
writeln("done.");
}
} else {
// OneDrive Business Account - always use a session to upload
writeln("");
@ -880,12 +888,12 @@ final class SyncEngine
}
}
writeln(" done.");
writeln("done.");
// As the session.upload includes the last modified time, save the response
saveItem(response);
}
log.fileOnly("Uploading file ", path, " ... done.");
// use the cTag instead of the eTag because Onedrive may update the metadata of files AFTER they have been uploaded
// use the cTag instead of the eTag because OneDrive may update the metadata of files AFTER they have been uploaded
eTag = response["cTag"].str;
}
if (accountType == "personal"){