Skip to content

Commit

Permalink
Adds more info to rest client when deserialization fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
ENDAVA\mzorec committed Jan 3, 2023
1 parent 5b7921a commit 8934198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/FlubuCore.WebApi.Client/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ protected async Task<T> GetResponse<T>(HttpResponseMessage response)
? JsonConvert.DeserializeObject<ErrorModel>(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)
Expand All @@ -217,8 +217,15 @@ protected async Task<T> GetResponse<T>(HttpResponseMessage response)

var jsonString = await response.Content.ReadAsStringAsync();

var result = JsonConvert.DeserializeObject<T>(jsonString);
return result;
try
{
var result = JsonConvert.DeserializeObject<T>(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()
Expand Down
6 changes: 6 additions & 0 deletions src/FlubuCore.WebApi.Client/WebApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit 8934198

Please sign in to comment.