Skip to content

Android HttpClientHandler throws wrong exception type #56089

@jonathanpeppers

Description

@jonathanpeppers

Description

Code such as this should throw a ArgumentOutOfRangeException:

new HttpClientHandler().MaxAutomaticRedirections = 0;

We have some tests checking this, and they throw TargetInvocationException instead:

07-21 08:08:26.036 30461 30478 I NUnit   : System.Reflection.TargetInvocationException : Arg_TargetInvocationException
07-21 08:08:26.036 30461 30478 I NUnit   :   ----> System.ArgumentOutOfRangeException : The specified value must be greater than 0 Arg_ParamName_Name, value
07-21 08:08:26.036 30461 30478 I NUnit   : ArgumentOutOfRange_ActualValue, 0
07-21 08:08:26.036 30461 30478 I NUnit   : 
07-21 08:08:26.036 30461 30478 I NUnit   :    at System.Reflection.RuntimeMethodInfo.Invoke(Object , BindingFlags , Binder , Object[] , CultureInfo ) in System.Private.CoreLib.dll:token 0x6002810+0x89
07-21 08:08:26.036 30461 30478 I NUnit   :    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) in System.Private.CoreLib.dll:token 0x6002779+0x0
07-21 08:08:26.036 30461 30478 I NUnit   :    at System.Net.Http.HttpClientHandler.InvokeNativeHandlerMethod(String , Object[] ) in System.Net.Http.dll:token 0x60001cd+0x2d
07-21 08:08:26.036 30461 30478 I NUnit   :    at System.Net.Http.HttpClientHandler.SetMaxAutomaticRedirections(Int32 ) in System.Net.Http.dll:token 0x60001dc+0x0
07-21 08:08:26.036 30461 30478 I NUnit   :    at System.Net.Http.HttpClientHandler.set_MaxAutomaticRedirections(Int32 ) in System.Net.Http.dll:token 0x60001da+0x7
07-21 08:08:26.036 30461 30478 I NUnit   :    at Xamarin.Android.NetTests.HttpClientHandlerTestBase.Properties_Invalid() in Mono.Android.NET-Tests.dll:token 0x600001c+0x7
...

We might need exception handling here to throw the inner exception if it hits a TargetInvocationException:

private object InvokeNativeHandlerMethod(string name, params object?[] parameters)
{
MethodInfo? method;
if (!s_cachedMethods.TryGetValue(name, out method))
{
method = _nativeHandler!.GetType()!.GetMethod(name);
s_cachedMethods[name] = method;
}
return method!.Invoke(_nativeHandler, parameters)!;
}

Configuration

dotnet --version
6.0.100-rc.1.21369.3

I believe this also happens in Preview 7.

Regression?

I believe this was working in Preview 6, because the Android http handler implementation was not complete yet.

/cc @steveisok

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions