Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 409 conflict handling for creating a webhook subscription #2502

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/onedrive.d
Original file line number Diff line number Diff line change
Expand Up @@ -1054,12 +1054,21 @@ final class OneDriveApi
try {
response = post(url, request.toString());
} catch (OneDriveException e) {
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));

// We need to exit here, user needs to fix issue
log.error("ERROR: Unable to initialize subscriptions for updates. Please fix this issue.");
shutdown();
exit(-1);
// The OneDrive API returned that there was an error
if (e.httpStatusCode == 409) {
// If the API returns a 409, there is a conflict with a very specific subscription id
// Subscription Id <UUID> already exists for the requested combination
// The conflict <UUID> is in the error message
deleteConflictSubscription(e.msg);
createSubscription();
} else {
// Display the error message
displayOneDriveErrorMessage(e.msg, getFunctionName!({}));
// We need to exit here, user needs to fix issue
log.error("ERROR: Unable to initialize subscriptions for updates. Please fix this issue.");
shutdown();
exit(-1);
}
}

// Save important subscription metadata including id and expiration
Expand Down Expand Up @@ -1093,6 +1102,21 @@ final class OneDriveApi
del(url);
log.log("Deleted subscription");
}

private void deleteConflictSubscription(string message) {
log.log("The OneDrive API reported that the Subscription ID already exists for the requested combination, thus generating a conflict");
auto errorArray = splitLines(message);
JSONValue errorMessage = parseJSON(replace(message, errorArray[0], ""));
string errorReason = errorMessage["error"]["message"].str;
auto errorReasonArray = errorReason.split!isWhite;
string conflictUUID = errorReasonArray[2];
log.log("The Subscription ID that is in conflict is: ", conflictUUID);
log.log("Attempting to delete the conflict Subscription ID");
const(char)[] url;
url = subscriptionUrl ~ "/" ~ conflictUUID;
del(url);
log.log("Deleted the conflict Subscription ID");
}

private void redeemToken(const(char)[] authCode)
{
Expand Down