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

fix: typo in extension methods #108

Merged
merged 1 commit into from
Jun 4, 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 src/BB84.Notifications/Commands/AsyncActionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public bool CanExecute(object? parameter)

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

/// <inheritdoc/>
public void RaiseCanExecuteChanged()
Expand Down Expand Up @@ -80,7 +80,7 @@ public bool CanExecute(object? parameter)

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

/// <inheritdoc/>
public async Task ExecuteAsync(T parameter)
Expand Down
6 changes: 3 additions & 3 deletions src/BB84.Notifications/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ 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"/>.
/// if an <see cref="Exception"/> occurs 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)
public static async void FireAndForgetSafeAsync(this Task task, IExceptionHandler? handler = null)
{
try
{
await task;
}
catch (Exception ex)
{
handler?.Handle(ex);
handler?.HandleError(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface IExceptionHandler
/// 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);
void HandleError(Exception exception);
}
33 changes: 33 additions & 0 deletions tests/BB84.NotificationsTests/Extensions/TaskExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BB84.Notifications.Extensions;
using BB84.Notifications.Interfaces.Components;

namespace BB84.NotificationsTests.Extensions;

[TestClass]
[SuppressMessage("Usage", "CA2201", Justification = "UnitTest")]
public sealed class TaskExtensionsTests
{
[TestMethod]
public void FireAndForgetSafeAsyncTest()
{
Task task = Task.Factory.StartNew(() => { });

task.FireAndForgetSafeAsync();

Assert.IsNotNull(task);
}

[TestMethod]
public void FireAndForgetSafeAsyncExceptionHandledTest()
{
VoidHandler handler = new();
Task task = Task.Factory.StartNew(() => throw new Exception(""));

task.FireAndForgetSafeAsync(handler);
}

private sealed class VoidHandler : IExceptionHandler
{
public void HandleError(Exception exception) { }
}
}