1
votes

I am using HttpWebRequest/Response to call WEB API's, this works and I have a try/catch block to handle errors which mostly works, but the problem is one specific error causes my try/catch to crash.

This error

System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)

--- End of inner exception stack trace ---

at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)

at System.Threading.Tasks.ValueTask`1.get_Result()

at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Threading.Tasks.ValueTask`1.get_Result()

at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)

at System.Threading.Tasks.ValueTask`1.get_Result()

at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)

at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

is not being handled by this code in my catch clause and I dont understand why

 catch (WebException we)
            {

                JObject emptyObject = null;

                if (we.Status == WebExceptionStatus.Timeout)
                {
                    log.LogInformation($"xxx_V2 HttpWebRequest suffered a timeout for URL: {apiUrl} at: {DateTime.Now}");
                }
...... Removed non relevant code
}

This means that code further down will crash as they rely on headers being present in a response but of course I dont have a response since it timed out, thats why I am trying to catch it above.

1

1 Answers

0
votes

As I can see that the exception being raise is of type of **System.Net.Http.HttpRequestException** and exception handling you did for the type of WebException is one of the reason that you code is not getting executed in the catch block . Try to optimize you code in such a way that if you are explicitly handling any type of exception other than WebException then have a catch block with base class of exception which is like below:

Catch(Exception ex) {

}

The WebException class is thrown by classes descended from WebRequest and WebResponse that implement pluggable protocols for accessing the Internet.You can read about this more in details in below doc:

https://docs.microsoft.com/en-us/dotnet/api/system.net.webexception?view=netframework-4.8

Hope it helps and let me know if you need any help implementing it.

Then all your unhandled exception will be handled in this block