Skip to content

Commit

Permalink
Send a user friendly warning message when /ocr yandex fails
Browse files Browse the repository at this point in the history
  • Loading branch information
d4n3436 committed Oct 9, 2023
1 parent 2e8ab48 commit bd5e1b1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Apis/Yandex/YandexImageSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ public YandexImageSearch(HttpClient httpClient)

using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
string message = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new YandexException(message);
throw new YandexException(message, e);
}

await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/OcrModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task<RuntimeResult> OcrAsync(OcrEngine ocrEngine, string? url, IDis
catch (YandexException e)
{
_logger.LogWarning(e, "Failed to perform Yandex OCR to url {Url}", url);
return FergunResult.FromError(_localizer[e.Message], ephemeral, interaction);
return FergunResult.FromError(_localizer["YandexOCRError"], ephemeral, interaction);
}

if (string.IsNullOrWhiteSpace(text))
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/Modules.OcrModule.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,7 @@
<data name="YandexOCR" xml:space="preserve">
<value>OCR de Yandex</value>
</data>
<data name="YandexOCRError" xml:space="preserve">
<value>No fue posible obtener una imagen en la URL especificada. Usa otra URL o inténtalo más tarde.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Resources/Modules.OcrModule.resx
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,7 @@
<data name="YandexOCR" xml:space="preserve">
<value>Yandex OCR</value>
</data>
<data name="YandexOCRError" xml:space="preserve">
<value>Unable to find an image at the specified URL. Use a different URL or try again later.</value>
</data>
</root>

0 comments on commit bd5e1b1

Please sign in to comment.