+
Skip to content

Allows to add an exception handler for unhandled exceptions #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Libs/GLib-2.0/Public/UnhandledException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;

namespace GLib;

/// <summary>
/// Allows handling exceptions which can't cross the native code boundary and
/// would terminate the application.
/// </summary>
public static class UnhandledException
{
private static Action<Exception>? ExceptionHandler;

/// <summary>
/// Sets an action which is invoked if an exception is raised which reaches the native code boundary.
/// This can be used by applications to handle these exceptions gracefully
/// without terminating the application.
/// </summary>
/// <param name="handler">Invoked if an unhandled exception occurs</param>
public static void SetHandler(Action<Exception>? handler)
{
ExceptionHandler = handler;
}

/// <summary>
/// Call this method to invoke the exception handler. If there is no exception
/// handler registered the application is terminated.
/// </summary>
/// <remarks>
/// This method is not intended to be called by application code, and should only
/// be called by code which catches an exception that would otherwise terminate
/// the application by unwinding to the native code boundary.
/// </remarks>
public static void Raise(Exception e)
{
if (ExceptionHandler is null)
{
Console.Error.WriteLine($"{nameof(GLib.UnhandledException)} - unhandled exception: {e}");
Environment.Exit(1);
}
else
{
ExceptionHandler(e);
}
}
}
9 changes: 8 additions & 1 deletion src/Libs/GObject-2.0/Public/Closure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ private void InternalCallback(IntPtr closure, IntPtr returnValuePtr, uint nParam
.Select(valueHandle => new Value(valueHandle))
.ToArray();

_callback(returnValue, paramValues);
try
{
_callback(returnValue, paramValues);
}
catch (Exception e)
{
GLib.UnhandledException.Raise(e);
}
}

public void Dispose()
Expand Down
33 changes: 33 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/UnhandledExceptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GirTest.Tests;

[TestClass, TestCategory("BindingTest")]
public class UnhandledExceptionTest : Test
{

//Important: Be aware that an unhandled exception handler is
//set globally. In case of unit tests being executed in
//parallel this could lead to unintended side effects.

[TestMethod]
public void SupportsSignalHandlers()
{
var tester = ReturningSignalTester.New();
tester.OnReturnBool += (_, _) =>
{
throw new NotImplementedException();
};

bool exceptionCaught = false;
GLib.UnhandledException.SetHandler(e =>
{
exceptionCaught = e is NotImplementedException;
});

tester.EmitReturnBool().Should().Be(false);
exceptionCaught.Should().Be(true);
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载