From bdd69d50b2e04d325985e4536e66f616887596f7 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sun, 21 Apr 2024 11:02:42 +0200 Subject: [PATCH 1/2] feat: exception handler for async methods changes: - added the `IExceptionHandler` interface - added the `TaskExtensions` class - modified the `AsyncActionCommand` methods - modified the `AsyncActionCommand` methods --- .../Commands/AsyncActionCommand.cs | 28 ++++++------------- .../Extensions/TaskExtensions.cs | 27 ++++++++++++++++++ .../Components/IExceptionHandler.cs | 13 +++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 src/BB84.Notifications/Extensions/TaskExtensions.cs create mode 100644 src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs diff --git a/src/BB84.Notifications/Commands/AsyncActionCommand.cs b/src/BB84.Notifications/Commands/AsyncActionCommand.cs index b7c7d80..a84e2c7 100644 --- a/src/BB84.Notifications/Commands/AsyncActionCommand.cs +++ b/src/BB84.Notifications/Commands/AsyncActionCommand.cs @@ -1,4 +1,6 @@ -using BB84.Notifications.Interfaces.Commands; +using BB84.Notifications.Extensions; +using BB84.Notifications.Interfaces.Commands; +using BB84.Notifications.Interfaces.Components; namespace BB84.Notifications.Commands; @@ -7,17 +9,11 @@ namespace BB84.Notifications.Commands; /// /// The task to execute. /// The condition to execute. -public sealed class AsyncActionCommand(Func execute, Func? canExecute) : IAsyncActionCommand +/// The exception handler to use. +public sealed class AsyncActionCommand(Func execute, Func? canExecute = null, IExceptionHandler? handler = null) : IAsyncActionCommand { private bool _isExecuting; - /// - /// Initializes a new instance of the class that can always execute. - /// - /// The task to execute. - public AsyncActionCommand(Func execute) : this(execute, null) - { } - /// public event EventHandler? CanExecuteChanged; @@ -50,7 +46,7 @@ public bool CanExecute(object? parameter) /// public void Execute(object? parameter) - => ExecuteAsync().Wait(); + => ExecuteAsync().ToSaveVoid(handler); /// public void RaiseCanExecuteChanged() @@ -66,17 +62,11 @@ public void RaiseCanExecuteChanged() /// The generic type to work with. /// The task to execute. /// The condition to execute. -public sealed class AsyncActionCommand(Func execute, Func? canExecute) : IAsyncActionCommand +/// The exception handler to use. +public sealed class AsyncActionCommand(Func execute, Func? canExecute = null, IExceptionHandler? handler = null) : IAsyncActionCommand { private bool _isExecuting; - /// - /// Initializes a new instance of class that can always execute. - /// - /// The task to execute. - public AsyncActionCommand(Func execute) : this(execute, null) - { } - /// public event EventHandler? CanExecuteChanged; @@ -90,7 +80,7 @@ public bool CanExecute(object? parameter) /// public void Execute(object? parameter) - => ExecuteAsync((T)parameter!).Wait(); + => ExecuteAsync((T)parameter!).ToSaveVoid(handler); /// public async Task ExecuteAsync(T parameter) diff --git a/src/BB84.Notifications/Extensions/TaskExtensions.cs b/src/BB84.Notifications/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..2bec8cc --- /dev/null +++ b/src/BB84.Notifications/Extensions/TaskExtensions.cs @@ -0,0 +1,27 @@ +using BB84.Notifications.Interfaces.Components; + +namespace BB84.Notifications.Extensions; + +/// +/// The task extensions class. +/// +public static class TaskExtensions +{ + /// + /// Awaits the provided , uses the exception + /// if an occured and returns . + /// + /// The task to await. + /// Sends the exception to an exception handler. + public static async void ToSaveVoid(this Task task, IExceptionHandler? handler = null) + { + try + { + await task; + } + catch (Exception ex) + { + handler?.Handle(ex); + } + } +} diff --git a/src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs b/src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs new file mode 100644 index 0000000..b9d8f51 --- /dev/null +++ b/src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs @@ -0,0 +1,13 @@ +namespace BB84.Notifications.Interfaces.Components; + +/// +/// The exception handler interface. +/// +public interface IExceptionHandler +{ + /// + /// If an error occurs, it send the exception to an error handler. + /// + /// The exception to handle. + void Handle(Exception exception); +} From d6573a98941a6bda07dec9d0d26148cbcfe2f970 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sun, 21 Apr 2024 18:26:34 +0200 Subject: [PATCH 2/2] feat: exception handler for async methods changes: - version bump to `3.2.x` - small fixes closes #93 --- Directory.Build.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index b32de31..00a7aeb 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ 3 - 1 + 2 $([System.DateTime]::UtcNow.ToString("MMdd")) $([System.DateTime]::UtcNow.ToString("HHmm")) $(VersionMajor).$(VersionMinor).$(VersionPatch).$(VersionRevision) @@ -70,7 +70,7 @@ - +