You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When using refit to access an api that is not well documented or changes often results in Serialization Exceptions. Imagine the following Refit interface [Get("/myclass/{id}")] Task<ApiResponse<MyClass>> Get( Guid id);
When the api changes or a property in MyClass has a typo a serialization exception will occur. Even if I catch the Exception I can't access the full response message to find the errors in either my code or with the Api.
Describe the solution you'd like
I would like the serialization exception to be handled in such a way that the full response message is preserved and passed along. Arguably this should be with ApiResponse as it might be due to an error with the remote api although it see the problem with missing status code etc.
Describe alternatives you've considered
Change the return type to HttpResponseMessage (Real return type becomes unclear for people that haven't written the client)
A "wrapper" content serializer that puts a try-catch around the actual serialization (My current solution)
private class WrappingSerializer : IContentSerializer
{
private readonly IContentSerializer _serializer;
public WrappingSerializer(IContentSerializer serializer)
{
_serializer = serializer;
}
//nothing useful to add to the serialization exception in this case
public async Task<HttpContent> SerializeAsync<T>(T item) => await _serializer.SerializeAsync(item);
public async Task<T> DeserializeAsync<T>(HttpContent content)
{
var stringContent = await content.ReadAsStringAsync();
try
{
return await _serializer.DeserializeAsync<T>(content);
}
catch (Exception serializerException)
{
var message =
new StringBuilder($"Serialization failed with a {serializerException.GetType()}")
.AppendLine("Original Content was")
.Append(stringContent)
.ToString();
throw new SerializationException(message, serializerException);
}
}
}
Describe suggestions on how to achieve the feature
I think the above solution with the wrapping serializer would be ok if we didn't have to read the content twice in the no error case. Unfortunately the content is already consumed when the exception occurs but maybe accessing the original Response is easier from within Refit
Additional context
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
When using refit to access an api that is not well documented or changes often results in Serialization Exceptions. Imagine the following Refit interface
[Get("/myclass/{id}")] Task<ApiResponse<MyClass>> Get( Guid id);
When the api changes or a property in MyClass has a typo a serialization exception will occur. Even if I catch the Exception I can't access the full response message to find the errors in either my code or with the Api.
Describe the solution you'd like
I would like the serialization exception to be handled in such a way that the full response message is preserved and passed along. Arguably this should be with ApiResponse as it might be due to an error with the remote api although it see the problem with missing status code etc.
Describe alternatives you've considered
Describe suggestions on how to achieve the feature
I think the above solution with the wrapping serializer would be ok if we didn't have to read the content twice in the no error case. Unfortunately the content is already consumed when the exception occurs but maybe accessing the original Response is easier from within Refit
Additional context
The text was updated successfully, but these errors were encountered: