Handle object error response from OneDrive (Issue #464) (#465)

* Add specific check to ensure that variable is of type object
* Add error logging to indicate error response from OneDrive was received
* Update error logging in saveItem to match
* Add a '401 Unauthorized' event handler when querying OneDrive if the file exists
This commit is contained in:
abraunegg 2019-04-13 08:31:54 +10:00 committed by GitHub
parent ea22d8fef5
commit a2889098e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1746,6 +1746,12 @@ final class SyncEngine
// test if the local path exists on OneDrive
fileDetailsFromOneDrive = onedrive.getPathDetails(path);
} catch (OneDriveException e) {
if (e.httpStatusCode == 401) {
// OneDrive returned a 'HTTP/1.1 401 Unauthorized Error' - no error message logged
log.error("ERROR: OneDrive returned a 'HTTP 401 - Unauthorized' - gracefully handling error");
return;
}
if (e.httpStatusCode == 404) {
// The file was not found on OneDrive, need to upload it
write("Uploading new file ", path, " ...");
@ -1887,6 +1893,8 @@ final class SyncEngine
// even though some file systems (such as a POSIX-compliant file system) may consider them as different.
// Note that NTFS supports POSIX semantics for case sensitivity but this is not the default behavior.
// fileDetailsFromOneDrive has to be a valid object
if (fileDetailsFromOneDrive.object()){
// Check that 'name' is in the JSON response (validates data) and that 'name' == the path we are looking for
if (("name" in fileDetailsFromOneDrive) && (fileDetailsFromOneDrive["name"].str == baseName(path))) {
// OneDrive 'name' matches local path name
@ -1977,6 +1985,11 @@ final class SyncEngine
log.error("ERROR: To resolve, rename this local file: ", absolutePath(path));
log.log("Skipping uploading this new file: ", absolutePath(path));
}
} else {
// fileDetailsFromOneDrive is not valid JSON, an error was returned from OneDrive
log.error("ERROR: An error was returned from OneDrive and the resulting response is not a valid JSON object");
log.error("ERROR: Increase logging verbosity to assist determining why.");
}
} else {
// Skip file - too large
log.log("Skipping uploading this new file as it exceeds the maximum size allowed by OneDrive: ", path);
@ -2090,7 +2103,8 @@ final class SyncEngine
}
} else {
// log error
log.error("ERROR: OneDrive response not a valid JSON object");
log.error("ERROR: An error was returned from OneDrive and the resulting response is not a valid JSON object");
log.error("ERROR: Increase logging verbosity to assist determining why.");
}
}