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 @@ - + 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); +}