-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
How to handle Failure while connecting to API #273
Comments
Implement a custom HttpClientHandler |
@geocine is there an example for this somewhere? |
@mdawood1991 please take a look at the following issues:
|
silly but why isn´t a try/catch block enough? |
@ahmedalejo I am already using a try catch on the part where I am calling the service:
but when the API/server is down the exception is not caught. |
GetContentAs, won't it call the service? |
This is what is happening below. I have a try I don't want to add two types of catch wherever I make a server/api call - There must be a way to assign a generic exception handler to the refit service. |
There are two different things going on though...semantically, they are different exceptions. The For |
@onovotny yes - but there must be some sort of Exception handler I can do for the refit service - I don't want to add multiple catch statements whenever there is a API service call. |
@mdawood1991 Actually I opened an issue a wihle ago that relates to your issue #272 |
@geocine your issue is related to ApiException. ApiException gets called when there is a response other than 200 from the server. What I want to handle is API Access errors/exceptions e.g Http Timeout, Http server refused connection to socket, or website not available etc. |
This has been on my radar for a while. We should probably split A workaround in the meantime (if using C#6) is: catch(Exception ex) when (ex is ApiException || ex is WebException)
{
// do whatever you need to
} I've actually used something similar to deal with retries for transient errors (this was before I knew about Polly): public class TransientExceptionHelper
{
private static readonly ISet<HttpStatusCode> TransientErrorStatusCodes = new HashSet<HttpStatusCode>(new[]
{
HttpStatusCode.BadGateway,
HttpStatusCode.GatewayTimeout,
HttpStatusCode.InternalServerError,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.RequestTimeout
});
public static bool IsTransient(Exception exception)
{
var apiException = exception as ApiException;
if (apiException != null)
{
return TransientErrorStatusCodes.Contains(apiException.StatusCode);
}
return exception is HttpRequestException || exception is OperationCanceledException;
}
}
// Usage
catch (Exception transientEx) when (TransientExceptionHelper.IsTransient(transientEx))
{
// Handle retries
} |
@bennor I am using Polly like this:
Is this the best way to do it using Polly? |
@mdawood1991 I am fairly new at using Polly but I have been using it like this:
|
Is there a way to not throw an exception ? I mean, this will dramatically kill performances. Besides, it's not really an exception since we got a response. An exception would be the server not responding at all. |
I still don't get it why this library is throwing |
Any solution ??? |
@angelru new feature appeared - https://github.com/reactiveui/refit#handling-exceptions in
|
@rekosko It is not very clear to me how to use it |
Closing due to age. Please try Refit v6 and reopen if still an issue. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
How to I handle can't connect to the API exception.
Currently my app crashes due to the following exception not been handled.
This is my API Interface
Then this is how I initialize and call the service
The text was updated successfully, but these errors were encountered: