Ensure unhandled errors are handled in all configurations

This commit is contained in:
Ben Olden-Cooligan 2023-10-08 17:38:10 -07:00
parent 5d2ac21185
commit d935fb4a95
5 changed files with 31 additions and 27 deletions

View File

@ -9,6 +9,8 @@ public static class GtkEntryPoint
{
public static int Run(string[] args)
{
GLib.ExceptionManager.UnhandledException += UnhandledGtkException;
if (args.Length > 0 && args[0] is "cli" or "console")
{
return ConsoleEntryPoint.Run(args.Skip(1).ToArray(), new GtkImagesModule());
@ -20,4 +22,16 @@ public static class GtkEntryPoint
return GuiEntryPoint.Run(args, new GtkImagesModule(), new GtkModule());
}
private static void UnhandledGtkException(GLib.UnhandledExceptionArgs e)
{
if (e.IsTerminating)
{
Log.FatalException("An error occurred that caused the task to terminate.", e.ExceptionObject as Exception ?? new Exception());
}
else
{
Log.ErrorException("An unhandled error occurred.", e.ExceptionObject as Exception ?? new Exception());
}
}
}

View File

@ -18,23 +18,6 @@ public class GtkEtoPlatform : EtoPlatform
public override bool IsGtk => true;
public override void InitializePlatform()
{
GLib.ExceptionManager.UnhandledException += UnhandledGtkException;
}
private static void UnhandledGtkException(GLib.UnhandledExceptionArgs e)
{
if (e.IsTerminating)
{
Log.FatalException("An error occurred that caused the task to terminate.", e.ExceptionObject as Exception ?? new Exception());
}
else
{
Log.ErrorException("An unhandled error occurred.", e.ExceptionObject as Exception ?? new Exception());
}
}
public override Application CreateApplication()
{
var application = new Application(Platforms.Gtk);

View File

@ -9,6 +9,16 @@ public static class MacEntryPoint
{
public static int Run(string[] args)
{
Runtime.MarshalManagedException += (_, eventArgs) =>
{
Log.ErrorException("Marshalling managed exception", eventArgs.Exception);
eventArgs.ExceptionMode = MarshalManagedExceptionMode.ThrowObjectiveCException;
};
Runtime.MarshalObjectiveCException += (_, eventArgs) =>
{
Log.Error($"Marshalling ObjC exception: {eventArgs.Exception.Description}");
};
if (args.Length > 0 && args[0] is "cli" or "console")
{
return ConsoleEntryPoint.Run(args.Skip(1).ToArray(), new MacImagesModule());

View File

@ -19,16 +19,6 @@ public class MacEtoPlatform : EtoPlatform
// into a foreground process once we know we're not in worker or console mode. This ensures workers don't have
// a chance to show in the dock.
MacProcessHelper.TransformThisProcessToForeground();
Runtime.MarshalManagedException += (_, eventArgs) =>
{
Log.ErrorException("Marshalling managed exception", eventArgs.Exception);
eventArgs.ExceptionMode = MarshalManagedExceptionMode.ThrowObjectiveCException;
};
Runtime.MarshalObjectiveCException += (_, eventArgs) =>
{
Log.Error($"Marshalling ObjC exception: {eventArgs.Exception.Description}");
};
}
public override Application CreateApplication()

View File

@ -26,6 +26,7 @@ public static class ConsoleEntryPoint
new CommonModule(), imageModule, new ConsoleModule(options), new RecoveryModule(), new ContextModule());
Paths.ClearTemp();
TaskScheduler.UnobservedTaskException += UnhandledTaskException;
// Start a pending worker process
container.Resolve<IWorkerFactory>().Init(
@ -38,4 +39,10 @@ public static class ConsoleEntryPoint
return ((ConsoleErrorOutput) container.Resolve<ErrorOutput>()).HasError ? 1 : 0;
}
private static void UnhandledTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
Log.FatalException("An error occurred that caused the task to terminate.", e.Exception);
e.SetObserved();
}
}