From fcf85a9165f70821957a556bfd7d1fdf3e786c94 Mon Sep 17 00:00:00 2001 From: Alxandr Date: Sun, 28 Dec 2014 12:19:03 +0100 Subject: [PATCH 1/2] Wrap exceptions in HttpRequestException --- src/ModernHttpClient/Android/OkHttpNetworkHandler.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 3bf7bf1..77de5b5 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -113,12 +113,15 @@ protected override async Task SendAsync(HttpRequestMessage throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString())); } } - } catch (IOException ex) { - if (ex.Message.ToLowerInvariant().Contains("canceled")) { + } catch (Exception ex) { + // TODO: Don't catch everything, just catch relevant (figure out which are) + if (ex is IOException && ex.Message.ToLowerInvariant().Contains("canceled")) { throw new OperationCanceledException(); } - throw; + throw new HttpRequestException( + "Request failed", + ex); } var respBody = resp.Body(); From ec26be92c857a2fe4d81a3841a0d23867ccc377a Mon Sep 17 00:00:00 2001 From: Alxandr Date: Sun, 28 Dec 2014 14:22:03 +0100 Subject: [PATCH 2/2] Only catch IOExceptions --- src/ModernHttpClient/Android/OkHttpNetworkHandler.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 77de5b5..b992c25 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -12,6 +12,7 @@ using System.Security.Cryptography.X509Certificates; using System.Globalization; using Android.OS; +using Java.Net; namespace ModernHttpClient { @@ -113,14 +114,15 @@ protected override async Task SendAsync(HttpRequestMessage throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString())); } } - } catch (Exception ex) { - // TODO: Don't catch everything, just catch relevant (figure out which are) - if (ex is IOException && ex.Message.ToLowerInvariant().Contains("canceled")) { - throw new OperationCanceledException(); + } catch (UnknownHostException ex) { + throw new HttpRequestException("Unknown host", ex); + } catch (IOException ex) { + if (ex.Message.ToLowerInvariant().Contains("canceled")) { + throw new OperationCanceledException(ex.Message, ex); } throw new HttpRequestException( - "Request failed", + ex.Message, ex); }