diff --git a/BotNet.Services/BotCommands/Art.cs b/BotNet.Services/BotCommands/Art.cs index 77f7bea..68b8740 100644 --- a/BotNet.Services/BotCommands/Art.cs +++ b/BotNet.Services/BotCommands/Art.cs @@ -51,19 +51,19 @@ await botClient.SendPhotoAsync( photo: new InputFileStream(imageStream, "art.png"), replyToMessageId: message.MessageId, cancellationToken: cancellationToken); - } catch (ContentFilteredException) { - await botClient.SendTextMessageAsync( + } catch (ContentFilteredException exc) { + await botClient.EditMessageTextAsync( chatId: message.Chat.Id, - text: "Content filtered", + messageId: busyMessage.MessageId, + text: $"{exc.Message ?? "Content filtered."}", parseMode: ParseMode.Html, - replyToMessageId: message.MessageId, cancellationToken: cancellationToken); } catch { - await botClient.SendTextMessageAsync( + await botClient.EditMessageTextAsync( chatId: message.Chat.Id, + messageId: busyMessage.MessageId, text: "Could not generate art", parseMode: ParseMode.Html, - replyToMessageId: message.MessageId, cancellationToken: cancellationToken); } } catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) { diff --git a/BotNet.Services/BotCommands/OpenAI.cs b/BotNet.Services/BotCommands/OpenAI.cs index 72569fd..0256f38 100644 --- a/BotNet.Services/BotCommands/OpenAI.cs +++ b/BotNet.Services/BotCommands/OpenAI.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Linq; @@ -13,7 +12,6 @@ using BotNet.Services.OpenAI.Skills; using BotNet.Services.RateLimit; using BotNet.Services.Stability.Models; -using BotNet.Services.Stability.Skills; using Microsoft.Extensions.DependencyInjection; using RG.Ninja; using SkiaSharp; @@ -873,11 +871,18 @@ await botClient.SendPhotoAsync( replyToMessageId: message.MessageId, cancellationToken: cancellationToken ); - } catch (ContentFilteredException) { + } catch (ContentFilteredException exc) { await botClient.EditMessageTextAsync( chatId: busyMessage.Chat.Id, messageId: busyMessage.MessageId, - text: "Content filtered.", + text: $"{exc.Message ?? "Content filtered."}", + parseMode: ParseMode.Html + ); + } catch { + await botClient.EditMessageTextAsync( + chatId: busyMessage.Chat.Id, + messageId: busyMessage.MessageId, + text: "Failed to generate image.", parseMode: ParseMode.Html ); } diff --git a/BotNet.Services/Stability/Models/ContentFilteredException.cs b/BotNet.Services/Stability/Models/ContentFilteredException.cs index 29cd174..701b284 100644 --- a/BotNet.Services/Stability/Models/ContentFilteredException.cs +++ b/BotNet.Services/Stability/Models/ContentFilteredException.cs @@ -1,5 +1,11 @@ using System; namespace BotNet.Services.Stability.Models { - public sealed class ContentFilteredException : Exception { } + public sealed class ContentFilteredException : Exception { + public ContentFilteredException() { + } + + public ContentFilteredException(string? message) : base(message) { + } + } } diff --git a/BotNet.Services/Stability/Models/ErrorResponse.cs b/BotNet.Services/Stability/Models/ErrorResponse.cs new file mode 100644 index 0000000..fe8d47f --- /dev/null +++ b/BotNet.Services/Stability/Models/ErrorResponse.cs @@ -0,0 +1,7 @@ +namespace BotNet.Services.Stability.Models { + public sealed record ErrorResponse( + string? Id, + string? Message, + string? Name + ); +} diff --git a/BotNet.Services/Stability/StabilityClient.cs b/BotNet.Services/Stability/StabilityClient.cs index 7d6583b..76b400e 100644 --- a/BotNet.Services/Stability/StabilityClient.cs +++ b/BotNet.Services/Stability/StabilityClient.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; @@ -61,6 +62,10 @@ CancellationToken cancellationToken using HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode) { string error = await response.Content.ReadAsStringAsync(cancellationToken); + if (response.StatusCode == HttpStatusCode.BadRequest) { + ErrorResponse? errorResponse = JsonSerializer.Deserialize(error, SNAKE_CASE_SERIALIZER_OPTIONS); + throw new ContentFilteredException(errorResponse?.Message); + } _logger.LogError("Unable to generate image: {0}, HTTP Status Code: {1}", error, (int)response.StatusCode); response.EnsureSuccessStatusCode(); }