Resolve database assertion failure due to authentication HTTP/1.1 400 Bad Request (#246)

* Enhance initialisation error handling to catch 4xx errors
This commit is contained in:
abraunegg 2018-11-21 18:54:08 +11:00 committed by GitHub
parent eaedfac34a
commit fc162b94e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 25 deletions

View file

@ -144,10 +144,10 @@ function test_onedrive {
cd ..
mkdir -p ~/.config/onedrive/
echo $ODP > ~/.config/onedrive/refresh_token
./onedrive --synchronize --verbose --syncdir=~/OneDriveALT
./onedrive --synchronize --verbose --syncdir '~/OneDriveALT'
# OneDrive Cleanup
rm -rf ~/OneDriveALT/*
./onedrive --synchronize --verbose --syncdir=~/OneDriveALT
./onedrive --synchronize --verbose --syncdir '~/OneDriveALT'
fi
}

View file

@ -6,6 +6,7 @@ import std.file, std.json, std.path;
import std.regex;
import std.stdio, std.string, std.uni, std.uri;
import core.time, core.thread;
import core.stdc.stdlib;
import config, itemdb, onedrive, selective, upload, util;
static import log;
@ -179,36 +180,56 @@ final class SyncEngine
// Set accountType, defaultDriveId, defaultRootId & remainingFreeSpace once and reuse where possible
JSONValue oneDriveDetails;
// Need to catch 5xx server side errors at initialization
// Need to catch 400 or 5xx server side errors at initialization
try {
oneDriveDetails = onedrive.getDefaultDrive();
// Successfully got details from OneDrive without a server side error such as HTTP/1.1 504 Gateway Timeout
accountType = oneDriveDetails["driveType"].str;
defaultDriveId = oneDriveDetails["id"].str;
defaultRootId = onedrive.getDefaultRoot["id"].str;
remainingFreeSpace = oneDriveDetails["quota"]["remaining"].integer;
// Display accountType, defaultDriveId, defaultRootId & remainingFreeSpace for verbose logging purposes
log.vlog("Account Type: ", accountType);
log.vlog("Default Drive ID: ", defaultDriveId);
log.vlog("Default Root ID: ", defaultRootId);
log.vlog("Remaining Free Space: ", remainingFreeSpace);
// Check the local database to ensure the OneDrive Root details are in the database
checkDatabaseForOneDriveRoot();
// Check if there is an interrupted upload session
if (session.restore()) {
log.log("Continuing the upload session ...");
auto item = session.upload();
saveItem(item);
}
} catch (OneDriveException e) {
if (e.httpStatusCode == 400) {
// OneDrive responded with 400 error: Bad Request
log.error("\nERROR: OneDrive returned a 'HTTP 400 Bad Request' - Cannot Initialize Sync Engine");
// Check this
if (cfg.getValue("drive_id").length) {
log.error("ERROR: Check your 'drive_id' entry in your configuration file as it may be incorrect\n");
}
// Must exit here
exit(0);
}
if (e.httpStatusCode == 401) {
// HTTP request returned status code 401 (Unauthorized)
log.error("\nERROR: OneDrive returned a 'HTTP 401 Unauthorized' - Cannot Initialize Sync Engine");
log.error("ERROR: Check your configuration as your access token may be empty or invalid\n");
// Must exit here
exit(0);
}
if (e.httpStatusCode >= 500) {
// There was a HTTP 5xx Server Side Error
log.error("ERROR: OneDrive returned a 'HTTP 5xx Server Side Error' - Cannot Initialize Sync Engine");
// Must exit here
exit(0);
}
}
}
// Successfully got details from OneDrive without a server side error such as HTTP/1.1 504 Gateway Timeout
accountType = oneDriveDetails["driveType"].str;
defaultDriveId = oneDriveDetails["id"].str;
defaultRootId = onedrive.getDefaultRoot["id"].str;
remainingFreeSpace = oneDriveDetails["quota"]["remaining"].integer;
// Display accountType, defaultDriveId, defaultRootId & remainingFreeSpace for verbose logging purposes
log.vlog("Account Type: ", accountType);
log.vlog("Default Drive ID: ", defaultDriveId);
log.vlog("Default Root ID: ", defaultRootId);
log.vlog("Remaining Free Space: ", remainingFreeSpace);
// Check the local database to ensure the OneDrive Root details are in the database
checkDatabaseForOneDriveRoot();
// Check if there is an interrupted upload session
if (session.restore()) {
log.log("Continuing the upload session ...");
auto item = session.upload();
saveItem(item);
}
}
// Configure noRemoteDelete if function is called