Add better error handling for non-200 codes

Also add better json deserialization error handling
This commit is contained in:
Eric 2023-08-18 15:40:40 +10:00
parent c716fde17b
commit 70c56eadec
No known key found for this signature in database
2 changed files with 30 additions and 14 deletions

View File

@ -71,7 +71,7 @@ namespace DistTestCore.Codex
public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId) public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId)
{ {
return Http().HttpPostJson($"storage/request/{contentId}", request); return Http().HttpPostJson<CodexSalesRequestStorageRequest, string>($"storage/request/{contentId}", request);
} }
public CodexStoragePurchase GetPurchaseStatus(string purchaseId) public CodexStoragePurchase GetPurchaseStatus(string purchaseId)

View File

@ -1,5 +1,6 @@
using Logging; using Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Serialization = Newtonsoft.Json.Serialization;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Http.Json; using System.Net.Http.Json;
using Utils; using Utils;
@ -47,11 +48,16 @@ namespace DistTestCore
public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body) public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body)
{ {
var json = HttpPostJson(route, body); var response = HttpPostJson(route, body);
var json = Time.Wait(response.Content.ReadAsStringAsync());
if(!response.IsSuccessStatusCode) {
throw new HttpRequestException(json);
}
Log(GetUrl() + route, json);
return TryJsonDeserialize<TResponse>(json); return TryJsonDeserialize<TResponse>(json);
} }
public string HttpPostJson<TRequest>(string route, TRequest body) public HttpResponseMessage HttpPostJson<TRequest>(string route, TRequest body)
{ {
return Retry(() => return Retry(() =>
{ {
@ -59,10 +65,7 @@ namespace DistTestCore
var url = GetUrl() + route; var url = GetUrl() + route;
using var content = JsonContent.Create(body); using var content = JsonContent.Create(body);
Log(url, JsonConvert.SerializeObject(body)); Log(url, JsonConvert.SerializeObject(body));
var result = Time.Wait(client.PostAsync(url, content)); return Time.Wait(client.PostAsync(url, content));
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str;
}, $"HTTP-POST-JSON: {route}"); }, $"HTTP-POST-JSON: {route}");
} }
@ -95,16 +98,29 @@ namespace DistTestCore
public T TryJsonDeserialize<T>(string json) public T TryJsonDeserialize<T>(string json)
{ {
try var errors = new List<string>();
var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings(){
Error = delegate(object? sender, Serialization.ErrorEventArgs args)
{ {
return JsonConvert.DeserializeObject<T>(json)!; if (args.CurrentObject == args.ErrorContext.OriginalObject)
}
catch (Exception exception)
{ {
var msg = $"Failed to deserialize JSON: '{json}' with exception: {exception}"; errors.Add($"""
throw new InvalidOperationException(msg, exception); Member: '{args.ErrorContext.Member?.ToString() ?? "<null>"}'
Path: {args.ErrorContext.Path}
Error: {args.ErrorContext.Error.Message}
""");
args.ErrorContext.Handled = true;
} }
} }
});
if (errors.Count() > 0) {
throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}");
}
else if (deserialized == null) {
throw new JsonSerializationException($"Failed to deserialize JSON '{json}': resulting deserialized object is null");
}
return deserialized;
}
private string GetUrl() private string GetUrl()
{ {