From 8934198f6d6ae065773be98ab9d1885af6f3db2c Mon Sep 17 00:00:00 2001 From: "ENDAVA\\mzorec" Date: Tue, 3 Jan 2023 09:58:18 +0100 Subject: [PATCH] Adds more info to rest client when deserialization fails. --- src/FlubuCore.WebApi.Client/RestClient.cs | 15 +++++++++++---- src/FlubuCore.WebApi.Client/WebApiException.cs | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/FlubuCore.WebApi.Client/RestClient.cs b/src/FlubuCore.WebApi.Client/RestClient.cs index 733ad858..a674419c 100644 --- a/src/FlubuCore.WebApi.Client/RestClient.cs +++ b/src/FlubuCore.WebApi.Client/RestClient.cs @@ -193,9 +193,9 @@ protected async Task GetResponse(HttpResponseMessage response) ? JsonConvert.DeserializeObject(errorString) : new ErrorModel(); } - catch (Exception) + catch (Exception e) { - throw new WebApiException(HttpStatusCode.InternalServerError, errorString); + throw new WebApiException(HttpStatusCode.InternalServerError, $"Deserialization failed: StatusCode: {response.StatusCode} Headers: {response.Headers}, RequestMessage: {response.RequestMessage}, Content: {errorString}", e); } throw new WebApiException(response.StatusCode, errorString) @@ -217,8 +217,15 @@ protected async Task GetResponse(HttpResponseMessage response) var jsonString = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonString); - return result; + try + { + var result = JsonConvert.DeserializeObject(jsonString); + return result; + } + catch (Exception e) + { + throw new WebApiException(HttpStatusCode.InternalServerError, $"Deserialization failed: StatusCode: {response.StatusCode} Headers: {response.Headers}, RequestMessage: {response.RequestMessage}, Content: {jsonString}", e); + } } protected virtual void GetAllClientMethods() diff --git a/src/FlubuCore.WebApi.Client/WebApiException.cs b/src/FlubuCore.WebApi.Client/WebApiException.cs index b276fe32..2ac804b6 100644 --- a/src/FlubuCore.WebApi.Client/WebApiException.cs +++ b/src/FlubuCore.WebApi.Client/WebApiException.cs @@ -13,6 +13,12 @@ public WebApiException(HttpStatusCode statusCode, string message) StatusCode = statusCode; } + public WebApiException(HttpStatusCode statusCode, string message, Exception ex) + : base(message, ex) + { + StatusCode = statusCode; + } + public HttpStatusCode StatusCode { get; set; } public string ErrorCode { get; set; }