From 86649390843cd7e921b9621bbc72a02e3e0db417 Mon Sep 17 00:00:00 2001 From: David Bennett Date: Wed, 11 Sep 2024 10:02:21 -0700 Subject: [PATCH] Add HttpRequestException expected exception handling (#443) --- .../DataManager/GitHubDataManager.cs | 16 ++++++++++++++++ .../DataManager/GitHubDataManagerUpdate.cs | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/GitHubExtension/DataManager/GitHubDataManager.cs b/src/GitHubExtension/DataManager/GitHubDataManager.cs index 170e42a..edf2229 100644 --- a/src/GitHubExtension/DataManager/GitHubDataManager.cs +++ b/src/GitHubExtension/DataManager/GitHubDataManager.cs @@ -251,6 +251,16 @@ private async Task UpdateDataStoreAsync(DataStoreOperationParameters parameters, PruneObsoleteData(); SetLastUpdatedInMetaData(); } + catch (HttpRequestException httpEx) + { + // HttpRequestExceptions can happen when internet connection is + // down or various other network issues. + _log.Warning($"Http Request Exception: {httpEx.Message}"); + tx.Rollback(); + + // Rethrow so clients can catch/display appropriate UX. + throw; + } catch (Exception ex) { _log.Error(ex, $"Failed Updating DataStore for: {parameters}"); @@ -331,6 +341,12 @@ private async Task UpdateDataForRepositoryAsync(DataStoreOperationParameters par PruneObsoleteData(); SetLastUpdatedInMetaData(); } + catch (HttpRequestException) + { + // Higher layer will catch and log this. Suppress logging an error for this to keep log clean. + tx.Rollback(); + throw; + } catch (Exception ex) { // This is for catching any other unexpected error as well as any we throw. diff --git a/src/GitHubExtension/DataManager/GitHubDataManagerUpdate.cs b/src/GitHubExtension/DataManager/GitHubDataManagerUpdate.cs index c3944fe..491299b 100644 --- a/src/GitHubExtension/DataManager/GitHubDataManagerUpdate.cs +++ b/src/GitHubExtension/DataManager/GitHubDataManagerUpdate.cs @@ -25,6 +25,15 @@ public static async Task Update() { await UpdateDeveloperPullRequests(); } + catch (HttpRequestException httpEx) + { + // HttpRequestExceptions can happen when internet connection is + // down or various other network issues unrelated to this update. + // This is not an error in the extension or anything we can + // address. Log a warning so it is understood why the update did + // not occur, but otherwise keep the log clean. + _log.Warning($"Http Request Exception: {httpEx.Message}"); + } catch (Exception ex) { _log.Error(ex, "Update failed unexpectedly.");