mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-29 05:55:21 -04:00
Generalize HTTP exceptions
This commit is contained in:
parent
d3b311b88f
commit
5a57b4a6b1
5 changed files with 26 additions and 17 deletions
|
@ -84,7 +84,7 @@
|
|||
<Reference Include="PresentationFramework" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Exceptions\UnathorizedException.cs" />
|
||||
<Compile Include="Exceptions\HttpErrorStatusCodeException.cs" />
|
||||
<Compile Include="Messages\ShowErrorMessage.cs" />
|
||||
<Compile Include="Messages\ShowExportDoneMessage.cs" />
|
||||
<Compile Include="Messages\ShowSettingsMessage.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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace DiscordChatExporter.Exceptions
|
||||
{
|
||||
public class UnathorizedException : Exception
|
||||
{
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue