Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Access Full Response Message with Serialization Exceptions #1017

Open
j-bbr opened this issue Jan 2, 2021 · 0 comments
Open

feature: Access Full Response Message with Serialization Exceptions #1017

j-bbr opened this issue Jan 2, 2021 · 0 comments

Comments

@j-bbr
Copy link

j-bbr commented Jan 2, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant