Skip to content

Commit

Permalink
Non-recursive Unwrap.
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkcod committed May 14, 2015
1 parent 740e462 commit 7daf3ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 13 additions & 10 deletions Source/Core/ConeTestFailure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,20 @@ public void WriteTo(ISessionWriter writer) {
}

public static Exception Unwrap(Exception error) {
var invocationException = error as TargetInvocationException;
if(invocationException != null)
return Unwrap(invocationException.InnerException);
var innerExceptionsProp = error.GetType().GetProperty("InnerExceptions", BindingFlags.Instance | BindingFlags.Public);
if(innerExceptionsProp == null)
return error;
var innerExceptions = innerExceptionsProp.GetValue(error, null) as ICollection<Exception>;
if(innerExceptions != null && innerExceptions.Count == 1)
return Unwrap(innerExceptions.First());
for(;;) {
var invocationException = error as TargetInvocationException;
if(invocationException != null)
error = invocationException.InnerException;

return error;
var innerExceptionsProp = error.GetType().GetProperty("InnerExceptions", BindingFlags.Instance | BindingFlags.Public);
if(innerExceptionsProp == null)
return error;

var innerExceptions = innerExceptionsProp.GetValue(error, null) as ICollection<Exception>;
if(innerExceptions != null && innerExceptions.Count == 1)
error = innerExceptions.First();
else return error;
}
}

static IEnumerable<StackFrame> GetNestedStackFrames(Exception e) {
Expand Down
7 changes: 6 additions & 1 deletion Specs/Cone.Specs/Core/ConeTestFailureSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;

namespace Cone.Core
{
Expand All @@ -7,8 +8,12 @@ public class ConeTestFailureSpec
{
public void Unwraps_AggregateException_with_single_error() {
var inner = new Exception();

Check.That(() => ConeTestFailure.Unwrap(new AggregateException(inner)) == inner);
}

public void Unwraps_TargetInvoationException() {
var inner = new Exception();
Check.That(() => ConeTestFailure.Unwrap(new TargetInvocationException(inner)) == inner);
}
}
}

0 comments on commit 7daf3ca

Please sign in to comment.