Skip to content

Commit

Permalink
Added handling for too many request error
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Jul 31, 2024
1 parent bf6339c commit 2bbbe2d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Apps.XtrfCustomerPortal/Api/ApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using Apps.XtrfCustomerPortal.Constants;
using Apps.XtrfCustomerPortal.Models.Dtos;
Expand Down Expand Up @@ -94,6 +96,11 @@ private async Task<LoginDto> 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));
Expand All @@ -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, @"<h1>([^<]+)</h1>");
return match.Success ? match.Groups[1].Value : "Unknown HTML error";
}
}

0 comments on commit 2bbbe2d

Please sign in to comment.