From 27a6443024a0c1aeb387b40b7260505f340d1af3 Mon Sep 17 00:00:00 2001 From: Redth Date: Wed, 16 Apr 2014 16:42:14 -0400 Subject: [PATCH] HockeyApp - Updated sample and exception handling Implemented a more reliable way to raise managed exceptions in a way that HockeyApp can see them and include the .NET stack trace of the exception --- HockeyApp/binding/Additions.cs | 31 ++++++++++++++----- .../HockeyAppSampleiOS/AppDelegate.cs | 2 +- .../HockeyAppSampleiOS/HomeViewController.cs | 16 +++++++++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/HockeyApp/binding/Additions.cs b/HockeyApp/binding/Additions.cs index c64dc0c3..4be0417f 100644 --- a/HockeyApp/binding/Additions.cs +++ b/HockeyApp/binding/Additions.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; using MonoTouch.Foundation; +using MonoTouch.ObjCRuntime; namespace HockeyApp { @@ -49,16 +50,32 @@ public static void ThrowExceptionAsNative(object exception) } [DllImport(MonoTouch.Constants.FoundationLibrary, EntryPoint = "NSGetUncaughtExceptionHandler")] - private static extern IntPtr NSGetUncaughtExceptionHandler(); + static extern IntPtr NSGetUncaughtExceptionHandler(); private delegate void ReporterDelegate(IntPtr ex); - private static void ConvertToNsExceptionAndAbort(object e) - { - var nse = new NSException(".NET Exception", e.ToString(), null); - var uncaught = NSGetUncaughtExceptionHandler(); - var dele = (ReporterDelegate)Marshal.GetDelegateForFunctionPointer(uncaught, typeof(ReporterDelegate)); - dele(nse.Handle); +// static void ConvertToNsExceptionAndAbort(object e) +// { +// var nse = new NSException(".NET Exception", e.ToString(), null); +// var uncaught = NSGetUncaughtExceptionHandler(); +// var dele = (ReporterDelegate)Marshal.GetDelegateForFunctionPointer(uncaught, typeof(ReporterDelegate)); +// dele(nse.Handle); +// } + + static void ConvertToNsExceptionAndAbort(object e) + { + var name = "Managed Xamarin.iOS .NET Exception"; + var msg = e.ToString(); + + var ex = e as Exception; + if(ex != null) + name = string.Format("{0}: {1}", ex.GetType().FullName, ex.Message); + + name = name.Replace("%", "%%"); + msg = msg.Replace("%", "%%"); + var nse = new NSException(name, msg, null); + var sel = new Selector("raise"); + Messaging.void_objc_msgSend(nse.Handle, sel.Handle); } } } diff --git a/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/AppDelegate.cs b/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/AppDelegate.cs index a277f62b..6bc2e233 100644 --- a/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/AppDelegate.cs +++ b/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/AppDelegate.cs @@ -14,7 +14,7 @@ namespace HockeyAppSampleiOS [Register ("AppDelegate")] public partial class AppDelegate : UIApplicationDelegate { - const string HOCKEYAPP_APPID = "YOUR-HOCKEYAPP-APP-ID"; + const string HOCKEYAPP_APPID = "YOUR-HOCKEYAPP-APPID"; UINavigationController navController; HomeViewController homeViewController; diff --git a/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/HomeViewController.cs b/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/HomeViewController.cs index 0146e5f7..115d58b4 100644 --- a/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/HomeViewController.cs +++ b/HockeyApp/samples/HockeyAppSampleiOS/HockeyAppSampleiOS/HomeViewController.cs @@ -18,7 +18,7 @@ public override void ViewDidLoad () var hockey = BITHockeyManager.SharedHockeyManager; Root = new RootElement ("HockeyApp Sample") { - new Section() { + new Section { new StringElement("Check for Updates", () => { hockey.UpdateManager.CheckForUpdate(); }), @@ -29,9 +29,23 @@ public override void ViewDidLoad () hockey.FeedbackManager.ShowFeedbackComposeView(); }), new StringElement("Crashed Last Run:", hockey.CrashManager.DidCrashInLastSession.ToString()) + }, + new Section { + new StringElement("Throw Managed .NET Exception", () => { + + throw new HockeyAppSampleException("You intentionally caused a crash!"); + + }) } }; } } + + public class HockeyAppSampleException : System.Exception + { + public HockeyAppSampleException(string msg) : base(msg) + { + } + } }