Skip to content
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
64 changes: 32 additions & 32 deletions src/Wolfgang.TryPattern/Try.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,6 @@ public static Result Run(Action action)



/// <summary>
/// Executes the specified action asynchronously, catching any exception that may occur.
/// </summary>
/// <param name="action">The action to execute.</param>
/// <param name="token">The CancellationToken to monitor.</param>
/// <returns>
/// A <see cref="Task"/> of <see cref="Result"/> representing the asynchronous operation.
/// </returns>
public static async Task<Result> RunAsync(Action action, CancellationToken token = default)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

try
{
await Task.Run(action, token).ConfigureAwait(false);
return Result.Success();
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
return Result.Failure(ex.Message);
}
}



/// <summary>
/// Executes the specified function, catching any exception that may occur.
/// </summary>
Expand Down Expand Up @@ -117,6 +85,38 @@ public static Result<T> Run<T>(Func<T>? function)



/// <summary>
/// Executes the specified action asynchronously, catching any exception that may occur.
/// </summary>
/// <param name="action">The action to execute.</param>
/// <param name="token">The CancellationToken to monitor.</param>
/// <returns>
/// A <see cref="Task"/> of <see cref="Result"/> representing the asynchronous operation.
/// </returns>
public static async Task<Result> RunAsync(Action action, CancellationToken token = default)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

try
{
await Task.Run(action, token).ConfigureAwait(false);
return Result.Success();
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
return Result.Failure(ex.Message);
}
}



/// <summary>
/// Executes the specified function, catching any exception that may occur.
/// </summary>
Expand Down
6 changes: 2 additions & 4 deletions tests/Wolfgang.TryPattern.Tests/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public void Ctor_when_passed_false_and_no_message_throw_InvalidOperationExceptio



[Theory]
[InlineData(true)]
[InlineData(false)]
public void Success_sets_properties_correctly(bool succeeded)
[Fact]
public void Success_sets_properties_correctly()
{
// Act
var result = Result.Success();
Expand Down
Loading