Handle HttpClient errors

This commit is contained in:
Serghei Cebotari 2024-11-14 23:51:27 +03:00
parent b18e573da4
commit ded0520756

View File

@ -2,6 +2,7 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace RhSolutions.Services;
@ -36,8 +37,8 @@ public class YandexOcrClient : IOcrClient
_httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true");
using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
@ -69,6 +70,13 @@ public class YandexOcrClient : IOcrClient
return result;
}
}
}
else
{
string content = await response.Content.ReadAsStringAsync();
MessageBox.Show($"{response.StatusCode}: {content}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return Enumerable.Empty<object[,]>();
}
}