From fc162b94e20c199e98e3d593f63a1659c2047379 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Wed, 21 Nov 2018 18:54:08 +1100 Subject: [PATCH] Resolve database assertion failure due to authentication HTTP/1.1 400 Bad Request (#246) * Enhance initialisation error handling to catch 4xx errors --- .travis-ci.sh | 4 +-- src/sync.d | 67 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/.travis-ci.sh b/.travis-ci.sh index f7204b1b..c9a236f3 100644 --- a/.travis-ci.sh +++ b/.travis-ci.sh @@ -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 } diff --git a/src/sync.d b/src/sync.d index 4ea543da..36edb264 100644 --- a/src/sync.d +++ b/src/sync.d @@ -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