mirror of
https://github.com/abraunegg/onedrive
synced 2024-05-09 17:26:47 +02:00
* Catch when OneDrive throws an error response (40x, 50x) on initialising
This commit is contained in:
parent
27d0b9641e
commit
a4d72fc73c
|
@ -190,12 +190,23 @@ final class OneDriveApi
|
|||
while (!exists(responseUrl)) {
|
||||
Thread.sleep(dur!("msecs")(100));
|
||||
}
|
||||
response = cast(char[]) read(responseUrl);
|
||||
|
||||
// read response from OneDrive
|
||||
try {
|
||||
response = cast(char[]) read(responseUrl);
|
||||
} catch (OneDriveException e) {
|
||||
// exception generated
|
||||
displayOneDriveErrorMessage(e.msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
// try to remove old files
|
||||
try {
|
||||
std.file.remove(authUrl);
|
||||
std.file.remove(responseUrl);
|
||||
} catch (FileException e) {
|
||||
log.error("Cannot remove files ", authUrl, " ", responseUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// match the authorization code
|
||||
|
@ -422,7 +433,15 @@ final class OneDriveApi
|
|||
|
||||
private void acquireToken(const(char)[] postData)
|
||||
{
|
||||
JSONValue response = post(tokenUrl, postData);
|
||||
JSONValue response;
|
||||
|
||||
try {
|
||||
response = post(tokenUrl, postData);
|
||||
} catch (OneDriveException e) {
|
||||
// an error was generated
|
||||
displayOneDriveErrorMessage(e.msg);
|
||||
}
|
||||
|
||||
if (response.type() == JSONType.object) {
|
||||
if ("access_token" in response){
|
||||
accessToken = "bearer " ~ response["access_token"].str();
|
||||
|
@ -913,6 +932,21 @@ final class OneDriveApi
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse and display error message received from OneDrive
|
||||
private void displayOneDriveErrorMessage(string message) {
|
||||
log.error("\nERROR: OneDrive returned an error with the following message:");
|
||||
auto errorArray = splitLines(message);
|
||||
log.error(" Error Message: ", errorArray[0]);
|
||||
// Strip cause from error to leave a JSON
|
||||
JSONValue errorMessage = parseJSON(replace(message, errorArray[0], ""));
|
||||
// extra debug
|
||||
log.vdebug("Raw Error Data: ", message);
|
||||
log.vdebug("JSON Message: ", errorMessage);
|
||||
if (errorMessage.type() == JSONType.object) {
|
||||
log.error(" Error Reason: ", errorMessage["error_description"].str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
|
|
Loading…
Reference in a new issue