Skip to content

Commit

Permalink
Better error handling for filtered content
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 5, 2023
1 parent b53a90c commit d0b9feb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
12 changes: 6 additions & 6 deletions BotNet.Services/BotCommands/Art.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<code>Content filtered</code>",
messageId: busyMessage.MessageId,
text: $"<code>{exc.Message ?? "Content filtered."}</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch {
await botClient.SendTextMessageAsync(
await botClient.EditMessageTextAsync(
chatId: message.Chat.Id,
messageId: busyMessage.MessageId,
text: "<code>Could not generate art</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
}
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
Expand Down
13 changes: 9 additions & 4 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
Expand All @@ -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;
Expand Down Expand Up @@ -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: "<code>Content filtered.</code>",
text: $"<code>{exc.Message ?? "Content filtered."}</code>",
parseMode: ParseMode.Html
);
} catch {
await botClient.EditMessageTextAsync(
chatId: busyMessage.Chat.Id,
messageId: busyMessage.MessageId,
text: "<code>Failed to generate image.</code>",
parseMode: ParseMode.Html
);
}
Expand Down
8 changes: 7 additions & 1 deletion BotNet.Services/Stability/Models/ContentFilteredException.cs
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
7 changes: 7 additions & 0 deletions BotNet.Services/Stability/Models/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotNet.Services.Stability.Models {
public sealed record ErrorResponse(
string? Id,
string? Message,
string? Name
);
}
5 changes: 5 additions & 0 deletions BotNet.Services/Stability/StabilityClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
Expand Down Expand Up @@ -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<ErrorResponse>(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();
}
Expand Down

0 comments on commit d0b9feb

Please sign in to comment.