From 141c8835d0c83cead3c55b8952b4b8173415aa25 Mon Sep 17 00:00:00 2001 From: Archi Date: Tue, 28 Dec 2021 13:55:18 +0100 Subject: [PATCH] Add error handling to inventory response on 5xx --- .../Steam/Integration/ArchiWebHandler.cs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs index 191326d6d..063088f48 100644 --- a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs +++ b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs @@ -168,10 +168,32 @@ public sealed class ArchiWebHandler : IDisposable { await ASF.InventorySemaphore.WaitAsync().ConfigureAwait(false); - ObjectResponse? response; + ObjectResponse? response = null; try { - response = await UrlGetToJsonObjectWithSession(request, rateLimitingDelay: rateLimitingDelay).ConfigureAwait(false); + for (byte i = 0; (i < WebBrowser.MaxTries) && (response == null); i++) { + if ((i > 0) && (rateLimitingDelay > 0)) { + await Task.Delay(rateLimitingDelay).ConfigureAwait(false); + } + + response = await UrlGetToJsonObjectWithSession(request, requestOptions: WebBrowser.ERequestOptions.ReturnServerErrors, rateLimitingDelay: rateLimitingDelay).ConfigureAwait(false); + + if (response == null) { + throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(response))); + } + + if (response.StatusCode.IsServerErrorCode()) { + if (string.IsNullOrEmpty(response.Content.Error)) { + // This is a generic server error without a reason, try again + response = null; + + continue; + } + + // This is actually client error with a reason, so it doesn't make sense to retry + throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.Content.Error)); + } + } } finally { if (rateLimitingDelay == 0) { ASF.InventorySemaphore.Release();