Skip to content

Commit 8934198

Browse files
author
ENDAVA\mzorec
committed
Adds more info to rest client when deserialization fails.
1 parent 5b7921a commit 8934198

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/FlubuCore.WebApi.Client/RestClient.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ protected async Task<T> GetResponse<T>(HttpResponseMessage response)
193193
? JsonConvert.DeserializeObject<ErrorModel>(errorString)
194194
: new ErrorModel();
195195
}
196-
catch (Exception)
196+
catch (Exception e)
197197
{
198-
throw new WebApiException(HttpStatusCode.InternalServerError, errorString);
198+
throw new WebApiException(HttpStatusCode.InternalServerError, $"Deserialization failed: StatusCode: {response.StatusCode} Headers: {response.Headers}, RequestMessage: {response.RequestMessage}, Content: {errorString}", e);
199199
}
200200

201201
throw new WebApiException(response.StatusCode, errorString)
@@ -217,8 +217,15 @@ protected async Task<T> GetResponse<T>(HttpResponseMessage response)
217217

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

220-
var result = JsonConvert.DeserializeObject<T>(jsonString);
221-
return result;
220+
try
221+
{
222+
var result = JsonConvert.DeserializeObject<T>(jsonString);
223+
return result;
224+
}
225+
catch (Exception e)
226+
{
227+
throw new WebApiException(HttpStatusCode.InternalServerError, $"Deserialization failed: StatusCode: {response.StatusCode} Headers: {response.Headers}, RequestMessage: {response.RequestMessage}, Content: {jsonString}", e);
228+
}
222229
}
223230

224231
protected virtual void GetAllClientMethods()

src/FlubuCore.WebApi.Client/WebApiException.cs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public WebApiException(HttpStatusCode statusCode, string message)
1313
StatusCode = statusCode;
1414
}
1515

16+
public WebApiException(HttpStatusCode statusCode, string message, Exception ex)
17+
: base(message, ex)
18+
{
19+
StatusCode = statusCode;
20+
}
21+
1622
public HttpStatusCode StatusCode { get; set; }
1723

1824
public string ErrorCode { get; set; }

0 commit comments

Comments
 (0)