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

Custom exception factory for deserialization exceptions #1815

Open
ted-ccm opened this issue Sep 16, 2024 · 0 comments
Open

Custom exception factory for deserialization exceptions #1815

ted-ccm opened this issue Sep 16, 2024 · 0 comments
Labels

Comments

@ted-ccm
Copy link
Contributor

ted-ccm commented Sep 16, 2024

Is your feature request related to a problem? Please describe.

Currently there is no way to suppress refit exception from including request parameters like headers and query string which could include sensitive information that needs to be prevented from being dumped into the log.

Describe the solution you'd like

Add an additional property in RefitSettings.cs similar to ExceptionFactory that allows overriding refit's default exception during deserialization exception.

Describe alternatives you've considered

Wrapping all requests with try and catch and throwing custom exception works but it is a bit clunky and risky as it there is a possibility that someone might rethrow the exception or forget to wrap the request in try and catch completely.

Describe suggestions on how to achieve the feature

Add a property in RefitSettings.cs...
public Func<HttpResponseMessage, Exception, Task<Exception?>>? DeserializationExceptionFactory { get; set; }

Use the factory in RequestBuilderImplementation.cs in BuildCancellableTaskFuncForMethod

Override both deserialization exceptions...

...
catch (Exception ex)
{
    //if an error occured while attempting to deserialize return the wrapped ApiException
    if (settings.DeserializationExceptionFactory != null)
        e = await settings.DeserializationExceptionFactory(resp, ex).ConfigureAwait(false);
    else
    {
        e = await ApiException.Create(
            "An error occured deserializing the response.",
            resp.RequestMessage!,
            resp.RequestMessage!.Method,
            resp,
            settings,
            ex
        );
    }
}
...
...
catch (Exception ex)
{
    if (settings.DeserializationExceptionFactory != null)
        throw await settings.DeserializationExceptionFactory(resp, ex).ConfigureAwait(false);
    else
    {
        throw await ApiException.Create(
            "An error occured deserializing the response.",
            resp.RequestMessage!,
            resp.RequestMessage!.Method,
            resp,
            settings,
            ex
        );
    }
}
...

Additional context

@ChrisPulman ChrisPulman transferred this issue from reactiveui/ReactiveUI Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants