I need to handle the timeout as a special case, but flurl does not give me expected exception on some cases.
Here is my simplified code to do so.
try
{
// Call Listener here with the result of the process
using (IFlurlClient flurlClient = new FlurlClient(Url))
{
string response = await flurlClient
.Configure(s => s.Timeout = TimeSpan.FromSeconds(130))
.WithOAuthBearerToken(accessToken)
.Request()
.PostJsonAsync(result)
.ReceiveString();
Logger.LogInformation(response);
ListenerResponse listenerResponse = JsonConvert.DeserializeObject<ListenerResponse>(response);
Logger.LogInformation(listenerResponse.Success.ToString());
if (!listenerResponse.Success)
{
throw new RejectedException("Listener rejected the request.");
}
}
}
catch (FlurlHttpTimeoutException ex)
{
// throws with this message: "Call timed out: POST https://..."
Logger.LogError(ex, $"Could not reach to Listener at: {Url}.");
throw;
}
catch (FlurlHttpException ex)
{
// throws with this message: "Call failed. Connection timed out POST https://..."
Logger.LogError(ex, ex.Message);
throw;
}
What's happening is when I set the timeout below a certain value (<130), flurl throws the expected FlurlHttpTimeoutException
. But if I set the timeout above that value(>=130), flurl this time throws the more general FlurlHttpException
.
Notice that the exception messages differ slightly:
FlurlHttpTimeoutException
Call timed out: POST https://...
FlurlHttpException
Call failed. Connection timed out POST https://...
Has anyone any idea how I can fix it to behave as expected - throwing FlurlHttpTimeoutException
for all timeout values?