From 5a57b4a6b10fcf003ee8d22c15866c2551964053 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Fri, 29 Sep 2017 20:24:02 +0300 Subject: [PATCH] Generalize HTTP exceptions --- DiscordChatExporter/DiscordChatExporter.csproj | 2 +- .../Exceptions/HttpErrorStatusCodeException.cs | 15 +++++++++++++++ .../Exceptions/UnathorizedException.cs | 8 -------- DiscordChatExporter/Services/DataService.cs | 7 +++---- DiscordChatExporter/ViewModels/MainViewModel.cs | 11 +++++++---- 5 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 DiscordChatExporter/Exceptions/HttpErrorStatusCodeException.cs delete mode 100644 DiscordChatExporter/Exceptions/UnathorizedException.cs diff --git a/DiscordChatExporter/DiscordChatExporter.csproj b/DiscordChatExporter/DiscordChatExporter.csproj index deaa1da..3260cc2 100644 --- a/DiscordChatExporter/DiscordChatExporter.csproj +++ b/DiscordChatExporter/DiscordChatExporter.csproj @@ -84,7 +84,7 @@ - + diff --git a/DiscordChatExporter/Exceptions/HttpErrorStatusCodeException.cs b/DiscordChatExporter/Exceptions/HttpErrorStatusCodeException.cs new file mode 100644 index 0000000..6cb562a --- /dev/null +++ b/DiscordChatExporter/Exceptions/HttpErrorStatusCodeException.cs @@ -0,0 +1,15 @@ +using System; +using System.Net; + +namespace DiscordChatExporter.Exceptions +{ + public class HttpErrorStatusCodeException : Exception + { + public HttpStatusCode StatusCode { get; } + + public HttpErrorStatusCodeException(HttpStatusCode statusCode) + { + StatusCode = statusCode; + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter/Exceptions/UnathorizedException.cs b/DiscordChatExporter/Exceptions/UnathorizedException.cs deleted file mode 100644 index 1ff9f04..0000000 --- a/DiscordChatExporter/Exceptions/UnathorizedException.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace DiscordChatExporter.Exceptions -{ - public class UnathorizedException : Exception - { - } -} \ No newline at end of file diff --git a/DiscordChatExporter/Services/DataService.cs b/DiscordChatExporter/Services/DataService.cs index 16c033e..c1aa2ad 100644 --- a/DiscordChatExporter/Services/DataService.cs +++ b/DiscordChatExporter/Services/DataService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Net.Http; using System.Threading.Tasks; using DiscordChatExporter.Exceptions; @@ -21,9 +20,9 @@ namespace DiscordChatExporter.Services using (var response = await _httpClient.GetAsync(url)) { // Check status code - if (response.StatusCode.IsEither(HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden)) - throw new UnathorizedException(); - response.EnsureSuccessStatusCode(); + // We throw our own exception here because default one doesn't have status code + if (!response.IsSuccessStatusCode) + throw new HttpErrorStatusCodeException(response.StatusCode); // Get content return await response.Content.ReadAsStringAsync(); diff --git a/DiscordChatExporter/ViewModels/MainViewModel.cs b/DiscordChatExporter/ViewModels/MainViewModel.cs index 57dd254..2778aa1 100644 --- a/DiscordChatExporter/ViewModels/MainViewModel.cs +++ b/DiscordChatExporter/ViewModels/MainViewModel.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Net; using DiscordChatExporter.Exceptions; using DiscordChatExporter.Messages; using DiscordChatExporter.Models; @@ -131,9 +132,10 @@ namespace DiscordChatExporter.ViewModels } } } - catch (UnathorizedException) + catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized) { - MessengerInstance.Send(new ShowErrorMessage("Failed to authorize. Make sure the token is valid.")); + const string message = "Could not authorize using the given token. Make sure it's valid."; + MessengerInstance.Send(new ShowErrorMessage(message)); } AvailableGuilds = _guildChannelsMap.Keys.ToArray(); @@ -181,9 +183,10 @@ namespace DiscordChatExporter.ViewModels // Show dialog MessengerInstance.Send(new ShowExportDoneMessage(sfd.FileName)); } - catch (UnathorizedException) + catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) { - MessengerInstance.Send(new ShowErrorMessage("Failed to export. You don't have access to that channel.")); + const string message = "You don't have access to the messages in that channel."; + MessengerInstance.Send(new ShowErrorMessage(message)); } IsBusy = false;