Refit: QUESTION - Exception Handling

Created on 6 Jun 2016  Â·  9Comments  Â·  Source: reactiveui/refit

I want to start using refit in my next project. I wonder if there is any clean way to handle all the exceptions thrown from one request instead of try catching it.

Imagine a special case where the request ends because internet wen't down. I wan't to deal with that and notify the user of that specific case.
I don't want to place all my requests inside a try catch.
Any suggestions?

outdated

Most helpful comment

Just change the method to this:

protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    try
    {                
        return await base.SendAsync(request, cancellationToken);
    }
    catch (Exception ex)
    {
        // get common exceptions here
    }
}

All 9 comments

Yes, place your requests in Try/Catch

@Cheesebaron the common error that I said above could happen in every request that I had in my application. Even if I use a generic method that encapsulates every call to have the Try/Catch to check the common erros, the code will have polluted with that.

I was asking this because refit could have a central error handling for cases like this.

You could write a custom HttpClientHandler that handles these kinds of errors

Thank you for the suggestion.
Looking at the HttpClientHandler the only overload that seems possible to intercept the request is the method SendAsync.

protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    try
    {                
        return base.SendAsync(request, cancellationToken);
    }
    catch (Exception ex)
    {
        // get common exceptions here
    }
}

The problem is that base.SendAsync(request, cancellationToken);
returns a Task<HttpResponseMessage>witch only return exception when awaited.

So, with this approach it seems to be impossible to do what i need.

So why don't you just await base.SendAsync(request, cancellationToken);?

I could but after that i just need to return a Task<HttpResponseMessage>.

Just change the method to this:

protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    try
    {                
        return await base.SendAsync(request, cancellationToken);
    }
    catch (Exception ex)
    {
        // get common exceptions here
    }
}

Yep, its ok now.

Now I'm facing another problem that is when the code executes the catch I must return something.
If i throw the exception i will be end in the same position as i start when writing this post. If I return null the refit request will throw.
What do you suggest in that case?

just return a new HttpResponseMessage() in the catch block and set
its properties as you please

On Mon, 6 Jun 2016 at 08:59 zeferino [email protected] wrote:

Yep, its ok now.

Now I'm facing another problem that is when the code executes the catch I
must return something.
If i throw the exception i will be end in the same position as i start
when writing this post. If I return null the refit request will throw.
What do you suggest in that case?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/paulcbetts/refit/issues/240#issuecomment-223938877,
or mute the thread
https://github.com/notifications/unsubscribe/AGr-6h07Ptqvb5-IMD6LyAKGKIsgatLHks5qJAuzgaJpZM4Ius-t
.

Was this page helpful?
0 / 5 - 0 ratings