From 2bbbe2d0351f13b531c18941dd9e1e834658f656 Mon Sep 17 00:00:00 2001 From: vitalii-bezuhlyi Date: Wed, 31 Jul 2024 10:45:15 +0300 Subject: [PATCH] Added handling for too many request error --- Apps.XtrfCustomerPortal/Api/ApiClient.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Apps.XtrfCustomerPortal/Api/ApiClient.cs b/Apps.XtrfCustomerPortal/Api/ApiClient.cs index 18bb946..343fb88 100644 --- a/Apps.XtrfCustomerPortal/Api/ApiClient.cs +++ b/Apps.XtrfCustomerPortal/Api/ApiClient.cs @@ -1,3 +1,5 @@ +using System.Net; +using System.Text.RegularExpressions; using System.Xml.Serialization; using Apps.XtrfCustomerPortal.Constants; using Apps.XtrfCustomerPortal.Models.Dtos; @@ -94,6 +96,11 @@ private async Task GetTokenAsync() protected override Exception ConfigureErrorException(RestResponse response) { + if (response.StatusCode == HttpStatusCode.TooManyRequests) + { + return new Exception(ParseHtmlErrorMessage(response.Content!)); + } + try { var xmlSerializer = new XmlSerializer(typeof(XmlErrorDto)); @@ -119,4 +126,10 @@ protected override Exception ConfigureErrorException(RestResponse response) return new Exception($"Unexpected error during error deserialization: {ex.Message}; Error body: {response.Content!} ; Status code: {response.StatusCode}"); } } + + private string ParseHtmlErrorMessage(string htmlContent) + { + var match = Regex.Match(htmlContent, @"

([^<]+)

"); + return match.Success ? match.Groups[1].Value : "Unknown HTML error"; + } } \ No newline at end of file