Skip to content

Commit

Permalink
Adds more Initializers for Result.Failure for convenience reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmonty committed Sep 7, 2015
1 parent 788510b commit 09bab1f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Functional/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;

namespace Pagansoft.Functional
{
Expand Down Expand Up @@ -186,6 +187,39 @@ public static Result<TSuccess> Failure<TSuccess>(ExceptionWithContext failure)
{
return new FailureResult<TSuccess>(failure);
}

/// <summary>
/// Creates a Failure Result value.
/// </summary>
/// <param name="failureMessage">The failure message.</param>
/// <typeparam name="TSuccess">The type of the success value.</typeparam>
public static Result<TSuccess> Failure<TSuccess>(string failureMessage)
{
return Result.Failure<TSuccess>(new ExceptionWithContext(failureMessage, null));
}

/// <summary>
/// Creates a Failure Result value.
/// </summary>
/// <param name="failureMessage">The failure message.</param>
/// <param name="context">The context values.</param>
/// <typeparam name="TSuccess">The type of the success value.</typeparam>
public static Result<TSuccess> Failure<TSuccess>(string failureMessage, Dictionary<string, object> context)
{
return Result.Failure<TSuccess>(new ExceptionWithContext(failureMessage, context));
}

/// <summary>
/// Creates a Failure Result value.
/// </summary>
/// <param name="failureMessage">The failure message.</param>
/// <param name="innerException">The inner exception.</param>
/// <param name="context">The context values.</param>
/// <typeparam name="TSuccess">The type of the success value.</typeparam>
public static Result<TSuccess> Failure<TSuccess>(string failureMessage, Exception innerException, Dictionary<string, object> context)
{
return Result.Failure<TSuccess>(new ExceptionWithContext(failureMessage, innerException, context));
}
}
}

2 changes: 1 addition & 1 deletion Test/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void ToString_Returns_EmptyString_If_Success_Value_Is_Null()
[Test]
public void ToString_Returns_EmptyString_If_Failure_Value_Is_Null()
{
Result.Failure<string>(null).ToString().ShouldBeEmpty();
Result.Failure<string>((ExceptionWithContext)null).ToString().ShouldBeEmpty();
}

[Test]
Expand Down

0 comments on commit 09bab1f

Please sign in to comment.