-
-
Notifications
You must be signed in to change notification settings - Fork 747
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: Ability to catch all exceptions, throw no exception and return a result #1238
Comments
Hi, I found a possible issue in the class I made an WPF application to reproduce this problem, If you run the application and click on the button 'Click me' you'll see an exception in red, if you click on the 'switch button', and test again you'll see that the problem is fixed. |
I have the same issue |
Same issue too: any news on this subject please? |
I have the same issue too |
same issue |
Exactly same pb for me |
Sorry @clairernovotny, can we have some feedback on this issue please ? |
I have the same issue |
I have the same issue. |
I am also having this issue on Android + iOS Xamarin.MAUI apps. If we use refit, the above exceptions are triggered when there is no connection. We can work around this, but it's annoying when it could be handled by refit. |
Hello, I would like to give my solution to solve this issue.
Putting things together
public class HttpRequestExceptionHandler : HttpClientHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
return await base.SendAsync(request, cancellationToken);
}
catch (HttpRequestException exception)
{
// You can use other status codes, such as HttpStatusCode.GatewayTimeout etc.
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
{
Content = new StringContent(exception.Message),
RequestMessage = request,
};
}
}
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
return base.Send(request, cancellationToken);
}
catch (HttpRequestException exception)
{
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
{
Content = new StringContent(exception.Message),
RequestMessage = request,
};
}
}
}
public partial class Program
{
private static readonly HttpClient _http;
static Program()
{
var handler = new HttpRequestExceptionHandler();
_http = new HttpClient(handler) { BaseAddress = new Uri("https://api.github.com") };
}
private static async Task Main()
{
var gitHubApi = RestService.For<IGitHubApi>(_http);
var octocat = await gitHubApi.GetUser("octocat");
}
} |
Same issue. Any news? |
Hi Team, I use Refit" Version="7.1.2" For now I used a solution as given in code. ``
`` |
There are already several issues (now all closed unfortunately) related to Refit not being able to catch exceptions which are not a result of server response or due to the serialization or deserialization:
#719
#273
Here's an example of exceptions not currently caught by Refit:
These exceptions are NOT caught by the newer ExceptionFactory mechanism: https://github.com/reactiveui/refit#providing-a-custom-exceptionfactory
In a real app, as a developer, you cannot just ignore/swallow these exceptions. Many times you need to actually even show a message to the user so the user is aware. Therefore, instead of having to catch and handle these errors correctly in every app, I think it would be really very useful if correctly catching and handling all the exceptions is implemented right in the Refit library.
From an API change point of view, I'm not sure exactly how is the best to do this. @clairernovotny suggested here to have a new
ApiRequestException
which will ensure the new way will be completely backward compatible and doesn't break any of the existing apps. Which sounds good.The text was updated successfully, but these errors were encountered: