-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Tests/Libs/GirTest-0.1.Tests/UnhandledExceptionTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.