Skip to content

Commit

Permalink
Consolidates UnWrap and Fixes Bug #10
Browse files Browse the repository at this point in the history
  • Loading branch information
odytrice committed Jan 12, 2018
1 parent 9814f4d commit 07960ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 4 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ that tells the calling method that it will not throw an exception rather, any ex
Using `Operation` helps ensure that your applications can fail gracefully even in unforeseen circumstances. It's typically used at the boundries
between domains/layers in your application.eg. Between Calls from WebApi to Business Layer or between calls from your Business Layer to your DataAccess Layer

## Operation and Operation<T>
## The Operation Class

At the Heart of the library are two types. They are `Operation` and `Operation<T>`.
An Operation represents the output of a piece of computation. It has two states: Succeeded or Failed.
Expand Down Expand Up @@ -244,3 +244,6 @@ Task<int> t = op.AsTask();
```

Note that this is not the same as `Operation.Run` Execution will still block the running Thread

Documentation available here
[Github Readme](https://github.com/odytrice/Operation/blob/master/ReadMe.md)
15 changes: 0 additions & 15 deletions src/Operation/UnWrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,13 @@ public static partial class OperationExtensions
/// <summary>
/// Unwraps the Operation if Successful and Throws an Exception if an Operation Fails
/// </summary>
/// <param name="operation"></param>
/// <param name="message"></param>
[DebuggerHidden]
public static void Unwrap(this Operation operation, string message = null)
{
message = message ?? operation.Message;
if (operation.Succeeded == false)
throw (message == null)
? operation.GetException()
: new Exception(message);
}
/// <summary>
/// Unwraps the Operation if Successful and Throws an Exception if an Operation Fails
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="operation"></param>
/// <param name="message">Optional Message to Mask Original Error</param>
/// <returns></returns>
[DebuggerHidden]
public static T Unwrap<T>(this Operation<T> operation, string message = null)
{
message = message ?? operation.Message;
if (operation.Succeeded == false)
throw (message == null)
? operation.GetException()
Expand Down
23 changes: 23 additions & 0 deletions test/Tests/UnwrapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,28 @@ public void TestOperationResultUnwrap()
Assert.IsFalse(operation.Succeeded);
Assert.AreEqual(operation.Message, "The Error");
}

[TestMethod]
public void TestOperationReturnsOriginalException()
{
var operation = Operation.Create(() => throw new ArgumentNullException("Argument Error"));
try
{
operation.Unwrap();
}
catch(Exception ex)
{
Assert.AreEqual(typeof(ArgumentNullException), ex.GetType());
}
}

[TestMethod]
public void TestOperationReturnsSuccessResult()
{
var value = 10;
var operation = Operation.Success(value);
var result = operation.Unwrap();
Assert.AreEqual(value, result);
}
}
}

0 comments on commit 07960ff

Please sign in to comment.