Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Ripped out FlattenException into a separate extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Aug 25, 2015
1 parent e7b8e34 commit ee93bf3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
32 changes: 32 additions & 0 deletions src/Nancy/Helpers/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Nancy.Helpers
{
using System;

internal static class ExceptionExtensions
{
internal static Exception FlattenInnerExceptions(this Exception exception)
{
var aggregateException = exception as AggregateException;
if (aggregateException != null)
{
var flattenedAggregateException = aggregateException.Flatten();

//If we have more than one exception in the AggregateException
//we have to send all exceptions back in order not to swallow any exceptions.
if (flattenedAggregateException.InnerExceptions.Count > 1)
{
return flattenedAggregateException;
}

return flattenedAggregateException.InnerException.FlattenInnerExceptions();
}

if (exception != null && exception.InnerException != null)
{
return exception.InnerException.FlattenInnerExceptions();
}

return exception;
}
}
}
1 change: 1 addition & 0 deletions src/Nancy/Nancy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<Compile Include="Diagnostics\DefaultDiagnostics.cs" />
<Compile Include="Diagnostics\DiagnosticsViewRenderer.cs" />
<Compile Include="Helpers\CacheHelpers.cs" />
<Compile Include="Helpers\ExceptionExtensions.cs" />
<Compile Include="IncludeInNancyAssemblyScanningAttribute.cs" />
<Compile Include="IStaticContentProvider.cs" />
<Compile Include="Json\Converters\TupleConverter.cs" />
Expand Down
28 changes: 1 addition & 27 deletions src/Nancy/NancyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private Action<Task> HandleFaultedTask(NancyContext context, IPipelines pipeline
{
try
{
var flattenedException = FlattenException(t.Exception);
var flattenedException = t.Exception.FlattenInnerExceptions();
this.InvokeOnErrorHook(context, pipelines.OnError, flattenedException);
Expand Down Expand Up @@ -328,31 +328,5 @@ private void InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exc
context.Items[ERROR_EXCEPTION] = e;
}
}

internal static Exception FlattenException(Exception exception)
{
if (exception is AggregateException)
{
var aggregateException = exception as AggregateException;

var flattenedAggregateException = aggregateException.Flatten();

//If we have more than one exception in the AggregateException
//we have to send all exceptions back in order not to swallow any exceptions.
if (flattenedAggregateException.InnerExceptions.Count > 1)
{
return flattenedAggregateException;
}

return flattenedAggregateException.InnerException;
}

if (exception != null && exception.InnerException != null)
{
return FlattenException(exception.InnerException);
}

return exception;
}
}
}
5 changes: 3 additions & 2 deletions src/Nancy/NancyEngineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request
public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request request, Func<NancyContext, NancyContext> preRequest)
{
var task = nancyEngine.HandleRequest(request, preRequest, CancellationToken.None);

try
{
task.Wait();
}
catch (Exception ex)
{
var flattenedException = NancyEngine.FlattenException(ex);
throw flattenedException;
throw ex.FlattenInnerExceptions();
}

return task.Result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nancy/Routing/DefaultRequestDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private Response ResolveErrorResult(NancyContext context, Func<NancyContext, Exc
{
if (resolveResultOnError != null)
{
var flattenedException = NancyEngine.FlattenException(exception);
var flattenedException = exception.FlattenInnerExceptions();

var result = resolveResultOnError.Invoke(context, flattenedException);
if (result != null)
Expand Down

0 comments on commit ee93bf3

Please sign in to comment.