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,11 +540,15 @@ final class OneDriveApi
private void checkHttpCode(ref const JSONValue response) private void checkHttpCode(ref const JSONValue response)
{ {
if (http.statusLine.code == 412) {
throw new OneDriveException(http.statusLine.code, http.statusLine.reason);
} else {
if (http.statusLine.code / 100 != 2) { if (http.statusLine.code / 100 != 2) {
throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response); throw new OneDriveException(http.statusLine.code, http.statusLine.reason, response);
} }
} }
} }
}
unittest unittest
{ {

View file

@ -847,17 +847,25 @@ final class SyncEngine
return; 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) { if (e.httpStatusCode == 504) {
// HTTP request returned status code 504 (Gateway Timeout) // HTTP request returned status code 504 (Gateway Timeout)
// Try upload as a session // 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; else throw e;
} }
writeln("done."); writeln("done.");
} else { } else {
writeln(""); writeln("");
response = session.upload(path, item.driveId, item.parentId, baseName(path), eTag); response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag);
writeln("done."); writeln("done.");
} }
} else { } else {
@ -885,7 +893,7 @@ final class SyncEngine
saveItem(response); saveItem(response);
} }
log.fileOnly("Uploading file ", path, " ... done."); 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; eTag = response["cTag"].str;
} }
if (accountType == "personal"){ if (accountType == "personal"){