Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
HockeyApp - Updated sample and exception handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Redth committed Apr 16, 2014
1 parent 68499db commit 27a6443
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
31 changes: 24 additions & 7 deletions HockeyApp/binding/Additions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;

namespace HockeyApp
{
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}),
Expand All @@ -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)
{
}
}
}

0 comments on commit 27a6443

Please sign in to comment.