Skip to content

Commit

Permalink
Add ToErrorOr, Updated Docs & change else to return ErrorOr
Browse files Browse the repository at this point in the history
  • Loading branch information
amantinband committed Jan 6, 2024
1 parent 515ac3c commit a757b15
Show file tree
Hide file tree
Showing 8 changed files with 889 additions and 347 deletions.
637 changes: 346 additions & 291 deletions README.md

Large diffs are not rendered by default.

92 changes: 88 additions & 4 deletions src/ErrorOr.ElseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static partial class ErrorOrExtensions
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, TValue> onError)
public static async Task<ErrorOr<TValue>> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, TValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

Expand All @@ -23,7 +23,7 @@ public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, TValue onError)
public static async Task<ErrorOr<TValue>> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, TValue onError)
{
var result = await errorOr.ConfigureAwait(false);

Expand All @@ -37,7 +37,7 @@ public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Task<TValue>> onError)
public static async Task<ErrorOr<TValue>> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Task<TValue>> onError)
{
var result = await errorOr.ConfigureAwait(false);

Expand All @@ -51,7 +51,91 @@ public static async Task<TValue> ElseAsync<TValue>(this Task<ErrorOr<TValue>> er
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Task<TValue> onError)
public static async Task<ErrorOr<TValue>> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Task<TValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.ElseAsync(onError).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<ErrorOr<TValue>> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Error> onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.Else(onError);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<ErrorOr<TValue>> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, List<Error>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.Else(onError);
}

/// <summary>
/// If the state is error, the provided <paramref name="error"/> is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="error">The error to return.</param>
/// <returns>The given <paramref name="error"/>.</returns>
public static async Task<ErrorOr<TValue>> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Error error)
{
var result = await errorOr.ConfigureAwait(false);

return result.Else(error);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<ErrorOr<TValue>> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Task<Error>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.ElseAsync(onError).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<ErrorOr<TValue>> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Task<List<Error>>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.ElseAsync(onError).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<ErrorOr<TValue>> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Task<Error> onError)
{
var result = await errorOr.ConfigureAwait(false);

Expand Down
28 changes: 28 additions & 0 deletions src/ErrorOr.ToErrorOrExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// Creates an <see cref="ErrorOr{TValue}"/> instance with the given <paramref name="value"/>.
/// </summary>
public static ErrorOr<TValue> ToErrorOr<TValue>(this TValue value)
{
return value;
}

/// <summary>
/// Creates an <see cref="ErrorOr{TValue}"/> instance with the given <paramref name="error"/>.
/// </summary>
public static ErrorOr<TValue> ToErrorOr<TValue>(this Error error)
{
return error;
}

/// <summary>
/// Creates an <see cref="ErrorOr{TValue}"/> instance with the given <paramref name="error"/>.
/// </summary>
public static ErrorOr<TValue> ToErrorOr<TValue>(this List<Error> error)
{
return error;
}
}
98 changes: 94 additions & 4 deletions src/ErrorOr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,52 @@ public async Task<ErrorOr<TNextValue>> ThenAsync<TNextValue>(Func<TValue, Task<T
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public TValue Else(Func<List<Error>, TValue> onError)
public ErrorOr<TValue> Else(Func<List<Error>, Error> onError)
{
if (!IsError)
{
return Value;
}

return onError(Errors);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed and its result is returned.
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public ErrorOr<TValue> Else(Func<List<Error>, List<Error>> onError)
{
if (!IsError)
{
return Value;
}

return onError(Errors);
}

/// <summary>
/// If the state is error, the provided <paramref name="error"/> is returned.
/// </summary>
/// <param name="error">The error to return.</param>
/// <returns>The given <paramref name="error"/>.</returns>
public ErrorOr<TValue> Else(Error error)
{
if (!IsError)
{
return Value;
}

return error;
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed and its result is returned.
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public ErrorOr<TValue> Else(Func<List<Error>, TValue> onError)
{
if (!IsError)
{
Expand All @@ -378,7 +423,7 @@ public TValue Else(Func<List<Error>, TValue> onError)
/// </summary>
/// <param name="onError">The value to return if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public TValue Else(TValue onError)
public ErrorOr<TValue> Else(TValue onError)
{
if (!IsError)
{
Expand All @@ -393,7 +438,7 @@ public TValue Else(TValue onError)
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public async Task<TValue> ElseAsync(Func<List<Error>, Task<TValue>> onError)
public async Task<ErrorOr<TValue>> ElseAsync(Func<List<Error>, Task<TValue>> onError)
{
if (!IsError)
{
Expand All @@ -408,7 +453,52 @@ public async Task<TValue> ElseAsync(Func<List<Error>, Task<TValue>> onError)
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public async Task<TValue> ElseAsync(Task<TValue> onError)
public async Task<ErrorOr<TValue>> ElseAsync(Func<List<Error>, Task<Error>> onError)
{
if (!IsError)
{
return Value;
}

return await onError(Errors).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public async Task<ErrorOr<TValue>> ElseAsync(Func<List<Error>, Task<List<Error>>> onError)
{
if (!IsError)
{
return Value;
}

return await onError(Errors).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided <paramref name="error"/> is awaited and returned.
/// </summary>
/// <param name="error">The error to return if the state is error.</param>
/// <returns>The result from awaiting the given <paramref name="error"/>.</returns>
public async Task<ErrorOr<TValue>> ElseAsync(Task<Error> error)
{
if (!IsError)
{
return Value;
}

return await error.ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original <see cref="Value"/>.</returns>
public async Task<ErrorOr<TValue>> ElseAsync(Task<TValue> onError)
{
if (!IsError)
{
Expand Down
58 changes: 40 additions & 18 deletions src/ErrorOrFactory.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
namespace ErrorOr;

/// <summary>
/// Provides factory methods for creating instances of <see cref="ErrorOr{TValue}"/>.
/// </summary>
public static class ErrorOrFactory
{
/// <summary>
/// Creates a new instance of <see cref="ErrorOr{TValue}"/> with a value.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value to wrap.</param>
/// <returns>An instance of <see cref="ErrorOr{TValue}"/> containing the provided value.</returns>
public static ErrorOr<TValue> From<TValue>(TValue value)
{
return value;
}
}
namespace ErrorOr;

/// <summary>
/// Provides factory methods for creating instances of <see cref="ErrorOr{TValue}"/>.
/// </summary>
public static class ErrorOrFactory
{
/// <summary>
/// Creates a new instance of <see cref="ErrorOr{TValue}"/> with a value.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value to wrap.</param>
/// <returns>An instance of <see cref="ErrorOr{TValue}"/> containing the provided value.</returns>
public static ErrorOr<TValue> From<TValue>(TValue value)
{
return value;
}

/// <summary>
/// Creates a new instance of <see cref="ErrorOr{TValue}"/> with a value.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value to wrap.</param>
/// <returns>An instance of <see cref="ErrorOr{TValue}"/> containing the provided value.</returns>
public static ErrorOr<TValue> From<TValue>(Error value)
{
return value;
}

/// <summary>
/// Creates a new instance of <see cref="ErrorOr{TValue}"/> with a value.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value to wrap.</param>
/// <returns>An instance of <see cref="ErrorOr{TValue}"/> containing the provided value.</returns>
public static ErrorOr<TValue> From<TValue>(List<Error> value)
{
return value;
}
}
Loading

0 comments on commit a757b15

Please sign in to comment.