Skip to content

Commit c0cb3fd

Browse files
committed
Unified naming for methods with similar semantics
1 parent 325175b commit c0cb3fd

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/DotNext.Tests/Threading/Tasks/ConversionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public static async Task SuspendException()
5050
await Task.FromException(new Exception()).SuspendException(Predicate.Constant<Exception>(true)).ConfigureAwait(true);
5151
await ValueTask.FromException(new Exception()).SuspendException(Predicate.Constant<Exception>(true)).ConfigureAwait(true);
5252

53-
var result = await Task.FromException<int>(new ArithmeticException()).SuspendException().ConfigureAwait(true);
53+
var result = await Task.FromException<int>(new ArithmeticException()).CatchException().ConfigureAwait(true);
5454
False(result.IsSuccessful);
5555
IsType<ArithmeticException>(result.Error);
5656

57-
result = await ValueTask.FromException<int>(new ArithmeticException()).SuspendException().ConfigureAwait(true);
57+
result = await ValueTask.FromException<int>(new ArithmeticException()).CatchException().ConfigureAwait(true);
5858
False(result.IsSuccessful);
5959
IsType<ArithmeticException>(result.Error);
6060
}
@@ -80,11 +80,11 @@ public static async Task SuspendException2()
8080
[Fact]
8181
public static async Task ConvertExceptionToError()
8282
{
83-
var result = await Task.FromException<int>(new Exception()).SuspendException(ToError).ConfigureAwait(true);
83+
var result = await Task.FromException<int>(new Exception()).CatchException(ToError).ConfigureAwait(true);
8484
False(result.IsSuccessful);
8585
Equal(EnvironmentVariableTarget.Machine, result.Error);
8686

87-
result = await ValueTask.FromException<int>(new Exception()).SuspendException(ToError).ConfigureAwait(true);
87+
result = await ValueTask.FromException<int>(new Exception()).CatchException(ToError).ConfigureAwait(true);
8888
False(result.IsSuccessful);
8989
Equal(EnvironmentVariableTarget.Machine, result.Error);
9090

src/DotNext/Threading/Tasks/Conversion.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ public static async Task<TOutput> Convert<TInput, TOutput>(this Task<TInput> tas
7373
/// <param name="task">The task to convert.</param>
7474
/// <typeparam name="T">The type of the task.</typeparam>
7575
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
76+
[Obsolete("Use CatchException() instead.")]
7677
public static AwaitableResult<T> SuspendException<T>(this Task<T> task)
78+
=> CatchException(task);
79+
80+
/// <summary>
81+
/// Returns a task that never throws an exception.
82+
/// </summary>
83+
/// <param name="task">The task to convert.</param>
84+
/// <typeparam name="T">The type of the task.</typeparam>
85+
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
86+
public static AwaitableResult<T> CatchException<T>(this Task<T> task)
7787
{
7888
ArgumentNullException.ThrowIfNull(task);
7989

@@ -86,7 +96,16 @@ public static AwaitableResult<T> SuspendException<T>(this Task<T> task)
8696
/// <param name="task">The task to convert.</param>
8797
/// <typeparam name="T">The type of the task.</typeparam>
8898
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
89-
public static AwaitableResult<T> SuspendException<T>(this ValueTask<T> task) => new(task);
99+
[Obsolete("Use CatchException() instead.")]
100+
public static AwaitableResult<T> SuspendException<T>(this ValueTask<T> task) => CatchException(task);
101+
102+
/// <summary>
103+
/// Returns a task that never throws an exception.
104+
/// </summary>
105+
/// <param name="task">The task to convert.</param>
106+
/// <typeparam name="T">The type of the task.</typeparam>
107+
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
108+
public static AwaitableResult<T> CatchException<T>(this ValueTask<T> task) => new(task);
90109

91110
/// <summary>
92111
/// Returns a task that never throws an exception.
@@ -96,7 +115,21 @@ public static AwaitableResult<T> SuspendException<T>(this Task<T> task)
96115
/// <typeparam name="T">The type of the task.</typeparam>
97116
/// <typeparam name="TError">The type of the error.</typeparam>
98117
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
118+
[Obsolete("Use CatchException() instead.")]
99119
public static AwaitableResult<T, TError> SuspendException<T, TError>(this ValueTask<T> task,
120+
Converter<Exception, TError> converter)
121+
where TError : struct, Enum
122+
=> CatchException(task, converter);
123+
124+
/// <summary>
125+
/// Returns a task that never throws an exception.
126+
/// </summary>
127+
/// <param name="task">The task to convert.</param>
128+
/// <param name="converter">The exception converter.</param>
129+
/// <typeparam name="T">The type of the task.</typeparam>
130+
/// <typeparam name="TError">The type of the error.</typeparam>
131+
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
132+
public static AwaitableResult<T, TError> CatchException<T, TError>(this ValueTask<T> task,
100133
Converter<Exception, TError> converter)
101134
where TError : struct, Enum
102135
=> new(task, converter);
@@ -109,10 +142,28 @@ public static AwaitableResult<T, TError> SuspendException<T, TError>(this ValueT
109142
/// <typeparam name="T">The type of the task.</typeparam>
110143
/// <typeparam name="TError">The type of the error.</typeparam>
111144
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
145+
[Obsolete("Use CatchException() instead.")]
112146
public static AwaitableResult<T, TError> SuspendException<T, TError>(this Task<T> task,
113147
Converter<Exception, TError> converter)
114148
where TError : struct, Enum
115-
=> new(task, converter);
149+
=> CatchException(task, converter);
150+
151+
/// <summary>
152+
/// Returns a task that never throws an exception.
153+
/// </summary>
154+
/// <param name="task">The task to convert.</param>
155+
/// <param name="converter">The exception converter.</param>
156+
/// <typeparam name="T">The type of the task.</typeparam>
157+
/// <typeparam name="TError">The type of the error.</typeparam>
158+
/// <returns>The task that never throws an exception. Instead, the <see cref="Result{T}"/> contains an exception.</returns>
159+
public static AwaitableResult<T, TError> CatchException<T, TError>(this Task<T> task,
160+
Converter<Exception, TError> converter)
161+
where TError : struct, Enum
162+
{
163+
ArgumentNullException.ThrowIfNull(task);
164+
165+
return new(task, converter);
166+
}
116167

117168
/// <summary>
118169
/// Suspends the exception that can be raised by the task.

0 commit comments

Comments
 (0)