mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-23 11:16:59 -04:00
Improve error reporting on unexpected HTTP status code
This commit is contained in:
parent
bcf652edbe
commit
ddfbe51cfa
4 changed files with 52 additions and 39 deletions
|
@ -108,7 +108,7 @@ public class DiscordClient
|
||||||
if (botResponse.StatusCode != HttpStatusCode.Unauthorized)
|
if (botResponse.StatusCode != HttpStatusCode.Unauthorized)
|
||||||
return TokenKind.Bot;
|
return TokenKind.Bot;
|
||||||
|
|
||||||
throw DiscordChatExporterException.Unauthorized();
|
throw new DiscordChatExporterException("Authentication token is invalid.", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ValueTask<HttpResponseMessage> GetResponseAsync(
|
private async ValueTask<HttpResponseMessage> GetResponseAsync(
|
||||||
|
@ -129,10 +129,26 @@ public class DiscordClient
|
||||||
{
|
{
|
||||||
throw response.StatusCode switch
|
throw response.StatusCode switch
|
||||||
{
|
{
|
||||||
HttpStatusCode.Unauthorized => DiscordChatExporterException.Unauthorized(),
|
HttpStatusCode.Unauthorized => throw new DiscordChatExporterException(
|
||||||
HttpStatusCode.Forbidden => DiscordChatExporterException.Forbidden(),
|
"Authentication token is invalid.",
|
||||||
HttpStatusCode.NotFound => DiscordChatExporterException.NotFound(url),
|
true
|
||||||
_ => DiscordChatExporterException.FailedHttpRequest(response)
|
),
|
||||||
|
|
||||||
|
HttpStatusCode.Forbidden => throw new DiscordChatExporterException(
|
||||||
|
$"Request to '{url}' failed: forbidden."
|
||||||
|
),
|
||||||
|
|
||||||
|
HttpStatusCode.NotFound => throw new DiscordChatExporterException(
|
||||||
|
$"Request to '{url}' failed: not found."
|
||||||
|
),
|
||||||
|
|
||||||
|
_ => throw new DiscordChatExporterException(
|
||||||
|
$"""
|
||||||
|
Request to '{url}' failed: {response.StatusCode.ToString().ToSpaceSeparatedWords().ToLowerInvariant()}.
|
||||||
|
Response content: {await response.Content.ReadAsStringAsync(cancellationToken)}
|
||||||
|
""",
|
||||||
|
true
|
||||||
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Exceptions;
|
namespace DiscordChatExporter.Core.Exceptions;
|
||||||
|
|
||||||
public partial class DiscordChatExporterException : Exception
|
public class DiscordChatExporterException : Exception
|
||||||
{
|
{
|
||||||
public bool IsFatal { get; }
|
public bool IsFatal { get; }
|
||||||
|
|
||||||
|
@ -13,32 +12,3 @@ public partial class DiscordChatExporterException : Exception
|
||||||
IsFatal = isFatal;
|
IsFatal = isFatal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class DiscordChatExporterException
|
|
||||||
{
|
|
||||||
internal static DiscordChatExporterException FailedHttpRequest(HttpResponseMessage response)
|
|
||||||
{
|
|
||||||
var message = $@"
|
|
||||||
Failed to perform an HTTP request.
|
|
||||||
|
|
||||||
[Request]
|
|
||||||
{response.RequestMessage}
|
|
||||||
|
|
||||||
[Response]
|
|
||||||
{response}";
|
|
||||||
|
|
||||||
return new DiscordChatExporterException(message.Trim(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static DiscordChatExporterException Unauthorized() =>
|
|
||||||
new("Authentication token is invalid.", true);
|
|
||||||
|
|
||||||
internal static DiscordChatExporterException Forbidden() =>
|
|
||||||
new("Access is forbidden.");
|
|
||||||
|
|
||||||
internal static DiscordChatExporterException NotFound(string resourceId) =>
|
|
||||||
new($"Requested resource ({resourceId}) does not exist.");
|
|
||||||
|
|
||||||
internal static DiscordChatExporterException ChannelIsEmpty() =>
|
|
||||||
new("Channel is empty or contains no messages for the specified period.");
|
|
||||||
}
|
|
|
@ -20,11 +20,19 @@ public class ChannelExporter
|
||||||
{
|
{
|
||||||
// Check if the channel is empty
|
// Check if the channel is empty
|
||||||
if (request.Channel.LastMessageId is null)
|
if (request.Channel.LastMessageId is null)
|
||||||
throw DiscordChatExporterException.ChannelIsEmpty();
|
{
|
||||||
|
throw new DiscordChatExporterException(
|
||||||
|
"Channel does not contain any messages."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the 'after' boundary is valid
|
// Check if the 'after' boundary is valid
|
||||||
if (request.After is not null && request.Channel.LastMessageId < request.After)
|
if (request.After is not null && request.Channel.LastMessageId < request.After)
|
||||||
throw DiscordChatExporterException.ChannelIsEmpty();
|
{
|
||||||
|
throw new DiscordChatExporterException(
|
||||||
|
"Channel does not contain any messages within the specified period."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Build context
|
// Build context
|
||||||
var context = new ExportContext(_discord, request);
|
var context = new ExportContext(_discord, request);
|
||||||
|
@ -50,6 +58,10 @@ public class ChannelExporter
|
||||||
|
|
||||||
// Throw if no messages were exported
|
// Throw if no messages were exported
|
||||||
if (messageExporter.MessagesExported <= 0)
|
if (messageExporter.MessagesExported <= 0)
|
||||||
throw DiscordChatExporterException.ChannelIsEmpty();
|
{
|
||||||
|
throw new DiscordChatExporterException(
|
||||||
|
"Channel does not contain any matching messages within the specified period."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,6 +16,21 @@ public static class StringExtensions
|
||||||
? str[..charCount]
|
? str[..charCount]
|
||||||
: str;
|
: str;
|
||||||
|
|
||||||
|
public static string ToSpaceSeparatedWords(this string str)
|
||||||
|
{
|
||||||
|
var builder = new StringBuilder(str.Length * 2);
|
||||||
|
|
||||||
|
foreach (var c in str)
|
||||||
|
{
|
||||||
|
if (char.IsUpper(c) && builder.Length > 0)
|
||||||
|
builder.Append(' ');
|
||||||
|
|
||||||
|
builder.Append(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<Rune> GetRunes(this string str)
|
public static IEnumerable<Rune> GetRunes(this string str)
|
||||||
{
|
{
|
||||||
var lastIndex = 0;
|
var lastIndex = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue