Skip to content

Commit

Permalink
Custom Exception fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Oct 18, 2024
1 parent 9c71c93 commit 12efbfb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
10 changes: 10 additions & 0 deletions TinyInsights.TestApp/CustomException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TinyInsights.TestApp;

public class BaseException : Exception
{
}


public class CustomException : BaseException
{
}
10 changes: 8 additions & 2 deletions TinyInsights.TestApp/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@
Clicked="TrackHttpButton_OnClicked"
Text="Track http request" />
<Button
x:Name="CrashButtom"
Clicked="CrashButtom_OnClicked"
x:Name="CrashButton"
Clicked="CrashButton_OnClicked"
Text="Crash app" />

<Button
x:Name="CustomCrashButton"
Clicked="CustomCrashButton_Clicked"
Text="Crash app with custom exception" />

<Button Clicked="NewPageButton_Clicked" Text="Test page automatic page tracking" />

<Button Clicked="Button_Clicked" Text="clear gc" />
Expand Down
7 changes: 6 additions & 1 deletion TinyInsights.TestApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private async void TrackHttpButton_OnClicked(object? sender, EventArgs e)
}
}

private void CrashButtom_OnClicked(object? sender, EventArgs e)
private void CrashButton_OnClicked(object? sender, EventArgs e)
{
throw new Exception("Crash Boom Bang!");
}
Expand All @@ -102,4 +102,9 @@ private void Button_Clicked(object sender, EventArgs e)
{
Debug.WriteLine($"Memory: {GC.GetTotalMemory(true)}");
}

private void CustomCrashButton_Clicked(object sender, EventArgs e)
{
throw new CustomException();
}
}
1 change: 0 additions & 1 deletion TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ private static void OnAppearing(object? sender, Page e)
client.Context.GlobalProperties.TryAdd("AppBuildNumber", AppInfo.BuildString);
client.Context.GlobalProperties.TryAdd("OperatingSystemVersion", DeviceInfo.VersionString);

Task.Run(SendCrashes);

return client;
}
Expand Down
43 changes: 31 additions & 12 deletions TinyInsights/Crash.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;

namespace TinyInsights;

public class Crash
Expand All @@ -10,33 +12,50 @@ public Crash()

public Crash(Exception exception)
{
var type = exception.GetType();

Message = exception.Message;
StackTrace = exception.StackTrace;
ExceptionType = exception.GetType().ToString();
ExceptionType = type.ToString();
ExceptionAssembly = type.Assembly.FullName!;
Source = exception.Source;
}

public string Message { get; init; }
public string? Message { get; init; }
public string? StackTrace { get; init; }
public string ExceptionType { get; init; }
public string ExceptionAssembly { get; init; }
public string? Source { get; init; }

public Exception? GetException()
{
var type = Type.GetType(ExceptionType);

if(type is null)
try
{
return null;
Assembly assembly = Assembly.Load(ExceptionAssembly);
Type type = assembly.GetType(ExceptionType)!;

Exception? ex;

try
{
ex = Activator.CreateInstance(type, args: Message) as Exception;
}
catch (MissingMethodException)
{
ex = Activator.CreateInstance(type) as Exception;
}

if (ex is not null)
{
ex.Source = Source;
}

return ex;
}

var ex = Activator.CreateInstance(type, args: Message) as Exception;

if(ex is not null)
catch (Exception)
{
ex.Source = Source;
return null;
}

return ex;
}
}

0 comments on commit 12efbfb

Please sign in to comment.