From 792fc864e8a008df159ba347fae49412ef65489e Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 13 May 2022 21:14:03 +0200 Subject: [PATCH] [Runtime] Provide better diagnostics if we fail to invoke a method in CoreCLR. A stack trace like this isn't all that helpful: *** Terminating app due to uncaught exception 'System.Reflection.TargetException', reason: 'Object does not match target type. (System.Reflection.TargetException) at System.Reflection.RuntimeConstructorInfo.CheckConsistency(Object target) at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at ObjCRuntime.Runtime.InvokeMethod(MethodBase method, Object instance, IntPtr native_parameters) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.CoreCLR.cs:line 655 at ObjCRuntime.Runtime.InvokeMethod(MonoObject* methodobj, MonoObject* instanceobj, IntPtr native_parameters) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.CoreCLR.cs:line 552 at ObjCRuntime.Runtime.bridge_runtime_invoke_method(MonoObject* method, MonoObject* instance, IntPtr parameters, IntPtr& exception_gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/runtime/Delegates.generated.cs:line 1210 with this change we'll be told exactly which function we failed to call. --- src/ObjCRuntime/Runtime.CoreCLR.cs | 2 ++ tools/mtouch/Errors.designer.cs | 9 +++++++++ tools/mtouch/Errors.resx | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/src/ObjCRuntime/Runtime.CoreCLR.cs b/src/ObjCRuntime/Runtime.CoreCLR.cs index 3beaa11122e1..54fa69b6a57b 100644 --- a/src/ObjCRuntime/Runtime.CoreCLR.cs +++ b/src/ObjCRuntime/Runtime.CoreCLR.cs @@ -694,6 +694,8 @@ static object InvokeMethod (MethodBase method, object instance, IntPtr native_pa var ex = tie.InnerException ?? tie; // This will re-throw the original exception and preserve the stacktrace. ExceptionDispatchInfo.Capture (ex).Throw (); + } catch (Exception e) { + throw ErrorHelper.CreateError (8042, e, Errors.MX8042 /* An exception occurred while trying to invoke the function {0}: {1}. */, GetMethodFullName (method), e.Message); } // Copy any byref parameters back out again diff --git a/tools/mtouch/Errors.designer.cs b/tools/mtouch/Errors.designer.cs index 4a3b4830c713..9981c4001bb3 100644 --- a/tools/mtouch/Errors.designer.cs +++ b/tools/mtouch/Errors.designer.cs @@ -4104,5 +4104,14 @@ public static string MX8041 { return ResourceManager.GetString("MX8041", resourceCulture); } } + + /// + /// Looks up a localized string similar to An exception occurred while trying to invoke the function {0}: {1}.. + /// + public static string MX8042 { + get { + return ResourceManager.GetString("MX8042", resourceCulture); + } + } } } diff --git a/tools/mtouch/Errors.resx b/tools/mtouch/Errors.resx index 1925d70a3e47..2c1cadaed8ef 100644 --- a/tools/mtouch/Errors.resx +++ b/tools/mtouch/Errors.resx @@ -2164,4 +2164,11 @@ Unable to create an instance of the type {0}. + + An exception occurred while trying to invoke the function {0}: {1}. + + 0: name of function + 1: exception info + +