Skip to content
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

feat: exception handler for async methods #95

Merged
merged 2 commits into from
Apr 21, 2024
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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup Label="Versioning">
<VersionMajor>3</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionMinor>2</VersionMinor>
<VersionPatch>$([System.DateTime]::UtcNow.ToString("MMdd"))</VersionPatch>
<VersionRevision>$([System.DateTime]::UtcNow.ToString("HHmm"))</VersionRevision>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch).$(VersionRevision)</VersionPrefix>
Expand Down Expand Up @@ -70,7 +70,7 @@
<ItemGroup Condition="!$(MSBuildProjectName.EndsWith('Tests'))">
<InternalsVisibleTo Include="$(AssemblyName)Tests" Key="00240000048000009400000006020000002400005253413100040000010001001d279f3822ca86e5157254452c9b0bda97bfeef3ed7964a8626318a1e46449adb0b62b7ca2f37b461ab2f28d0203344b11da76c5244331ff0a8629a258901534d0fb7d1cdfc6c048031874fced4f4c6a6d87991d4105e072adde2a965ccd440bbf8c3f90d6de6f8cfce47bd8908d9fdcdc08c617dc04616fc1dfcd290b4f4eec" />
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\icon.png" Pack="True" PackagePath="" />
<None Include="..\..\icon.png" Pack="True" PackagePath="\" />
<None Include="..\..\README.md" Pack="True" PackagePath="\" />
</ItemGroup>

Expand Down
28 changes: 9 additions & 19 deletions src/BB84.Notifications/Commands/AsyncActionCommand.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -7,17 +9,11 @@ namespace BB84.Notifications.Commands;
/// </summary>
/// <param name="execute">The task to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class AsyncActionCommand(Func<Task> execute, Func<bool>? canExecute) : IAsyncActionCommand
/// <param name="handler">The exception handler to use.</param>
public sealed class AsyncActionCommand(Func<Task> execute, Func<bool>? canExecute = null, IExceptionHandler? handler = null) : IAsyncActionCommand
{
private bool _isExecuting;

/// <summary>
/// Initializes a new instance of the <see cref="AsyncActionCommand"/> class that can always execute.
/// </summary>
/// <param name="execute">The task to execute.</param>
public AsyncActionCommand(Func<Task> execute) : this(execute, null)
{ }

/// <inheritdoc/>
public event EventHandler? CanExecuteChanged;

Expand Down Expand Up @@ -50,7 +46,7 @@ public bool CanExecute(object? parameter)

/// <inheritdoc/>
public void Execute(object? parameter)
=> ExecuteAsync().Wait();
=> ExecuteAsync().ToSaveVoid(handler);

/// <inheritdoc/>
public void RaiseCanExecuteChanged()
Expand All @@ -66,17 +62,11 @@ public void RaiseCanExecuteChanged()
/// <typeparam name="T">The generic type to work with.</typeparam>
/// <param name="execute">The task to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class AsyncActionCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute) : IAsyncActionCommand<T>
/// <param name="handler">The exception handler to use.</param>
public sealed class AsyncActionCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute = null, IExceptionHandler? handler = null) : IAsyncActionCommand<T>
{
private bool _isExecuting;

/// <summary>
/// Initializes a new instance of <see cref="AsyncActionCommand{T}"/> class that can always execute.
/// </summary>
/// <param name="execute">The task to execute.</param>
public AsyncActionCommand(Func<T, Task> execute) : this(execute, null)
{ }

/// <inheritdoc/>
public event EventHandler? CanExecuteChanged;

Expand All @@ -90,7 +80,7 @@ public bool CanExecute(object? parameter)

/// <inheritdoc/>
public void Execute(object? parameter)
=> ExecuteAsync((T)parameter!).Wait();
=> ExecuteAsync((T)parameter!).ToSaveVoid(handler);

/// <inheritdoc/>
public async Task ExecuteAsync(T parameter)
Expand Down
27 changes: 27 additions & 0 deletions src/BB84.Notifications/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BB84.Notifications.Interfaces.Components;

namespace BB84.Notifications.Extensions;

/// <summary>
/// The task extensions class.
/// </summary>
public static class TaskExtensions
{
/// <summary>
/// Awaits the provided <paramref name="task"/>, uses the exception <paramref name="handler"/>
/// if an <see cref="Exception"/> occured and returns <see cref="void"/>.
/// </summary>
/// <param name="task">The task to await.</param>
/// <param name="handler">Sends the exception to an exception handler.</param>
public static async void ToSaveVoid(this Task task, IExceptionHandler? handler = null)
{
try
{
await task;
}
catch (Exception ex)
{
handler?.Handle(ex);
}
}
}
13 changes: 13 additions & 0 deletions src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BB84.Notifications.Interfaces.Components;

/// <summary>
/// The exception handler interface.
/// </summary>
public interface IExceptionHandler
{
/// <summary>
/// If an error occurs, it send the exception to an error handler.
/// </summary>
/// <param name="exception">The exception to handle.</param>
void Handle(Exception exception);
}