Skip to content

Commit

Permalink
[Mono.Android] Wrap connection exceptions in HttpRequestException (do…
Browse files Browse the repository at this point in the history
…tnet#7661)

Fixes: dotnet#7629

Whenever a Java backend connection fails for any reason, wrap the
thrown exception in `HttpRequestException` as described in the
[`HttpClient.SendAsync()` documentation][0].

[0]: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.sendasync?view=net-7.0#system-net-http-httpclient-sendasync(system-net-http-httprequestmessage)
  • Loading branch information
grendello authored Jan 28, 2023
1 parent c1752ca commit 1e45033
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,11 @@ Task ConnectAsync (HttpURLConnection httpConnection, CancellationToken ct)
Logger.Log (LogLevel.Info, LOG_APP, $"Exception caught while cancelling connection: {ex}");
ct.ThrowIfCancellationRequested ();
}
throw;
// All exceptions related to connectivity should be wrapped in HttpRequestException. In theory it is possible that the exception will be
// thrown for another reason above, but it's OK to wrap it in HttpRequestException anyway, since we're working in the context of
// `ConnectAsync` which, from the end user's point of view, is 100% related to connectivity.
throw new HttpRequestException ("Connection failure", ex);
}
}, ct);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ private async Task AssertRejectsRemoteCertificate (Func<Task> makeRequest)
Assert.Fail ("The request wasn't rejected");
}
#if NET
catch (System.Net.WebException ex) {}
// While technically we should be throwing only HttpRequestException (as per HttpClient.SendAsync docs), in reality
// we need to consider legacy code that migrated to .NET and may still expect WebException. Thus, we throw both
// of these and we need to catch both here
catch (System.Net.WebException) {}
#else
catch (Java.IO.IOException) {}
#endif
catch (System.Net.Http.HttpRequestException) {}
}
}
}

0 comments on commit 1e45033

Please sign in to comment.