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

Commit ee93bf3

Browse files
committed
Ripped out FlattenException into a separate extension method
1 parent e7b8e34 commit ee93bf3

File tree

5 files changed

+38
-30
lines changed

5 files changed

+38
-30
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Nancy.Helpers
2+
{
3+
using System;
4+
5+
internal static class ExceptionExtensions
6+
{
7+
internal static Exception FlattenInnerExceptions(this Exception exception)
8+
{
9+
var aggregateException = exception as AggregateException;
10+
if (aggregateException != null)
11+
{
12+
var flattenedAggregateException = aggregateException.Flatten();
13+
14+
//If we have more than one exception in the AggregateException
15+
//we have to send all exceptions back in order not to swallow any exceptions.
16+
if (flattenedAggregateException.InnerExceptions.Count > 1)
17+
{
18+
return flattenedAggregateException;
19+
}
20+
21+
return flattenedAggregateException.InnerException.FlattenInnerExceptions();
22+
}
23+
24+
if (exception != null && exception.InnerException != null)
25+
{
26+
return exception.InnerException.FlattenInnerExceptions();
27+
}
28+
29+
return exception;
30+
}
31+
}
32+
}

src/Nancy/Nancy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="Diagnostics\DefaultDiagnostics.cs" />
174174
<Compile Include="Diagnostics\DiagnosticsViewRenderer.cs" />
175175
<Compile Include="Helpers\CacheHelpers.cs" />
176+
<Compile Include="Helpers\ExceptionExtensions.cs" />
176177
<Compile Include="IncludeInNancyAssemblyScanningAttribute.cs" />
177178
<Compile Include="IStaticContentProvider.cs" />
178179
<Compile Include="Json\Converters\TupleConverter.cs" />

src/Nancy/NancyEngine.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private Action<Task> HandleFaultedTask(NancyContext context, IPipelines pipeline
275275
{
276276
try
277277
{
278-
var flattenedException = FlattenException(t.Exception);
278+
var flattenedException = t.Exception.FlattenInnerExceptions();
279279

280280
this.InvokeOnErrorHook(context, pipelines.OnError, flattenedException);
281281

@@ -328,31 +328,5 @@ private void InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exc
328328
context.Items[ERROR_EXCEPTION] = e;
329329
}
330330
}
331-
332-
internal static Exception FlattenException(Exception exception)
333-
{
334-
if (exception is AggregateException)
335-
{
336-
var aggregateException = exception as AggregateException;
337-
338-
var flattenedAggregateException = aggregateException.Flatten();
339-
340-
//If we have more than one exception in the AggregateException
341-
//we have to send all exceptions back in order not to swallow any exceptions.
342-
if (flattenedAggregateException.InnerExceptions.Count > 1)
343-
{
344-
return flattenedAggregateException;
345-
}
346-
347-
return flattenedAggregateException.InnerException;
348-
}
349-
350-
if (exception != null && exception.InnerException != null)
351-
{
352-
return FlattenException(exception.InnerException);
353-
}
354-
355-
return exception;
356-
}
357331
}
358332
}

src/Nancy/NancyEngineExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request
2828
public static NancyContext HandleRequest(this INancyEngine nancyEngine, Request request, Func<NancyContext, NancyContext> preRequest)
2929
{
3030
var task = nancyEngine.HandleRequest(request, preRequest, CancellationToken.None);
31+
3132
try
3233
{
3334
task.Wait();
3435
}
3536
catch (Exception ex)
3637
{
37-
var flattenedException = NancyEngine.FlattenException(ex);
38-
throw flattenedException;
38+
throw ex.FlattenInnerExceptions();
3939
}
40+
4041
return task.Result;
4142
}
4243

src/Nancy/Routing/DefaultRequestDispatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private Response ResolveErrorResult(NancyContext context, Func<NancyContext, Exc
137137
{
138138
if (resolveResultOnError != null)
139139
{
140-
var flattenedException = NancyEngine.FlattenException(exception);
140+
var flattenedException = exception.FlattenInnerExceptions();
141141

142142
var result = resolveResultOnError.Invoke(context, flattenedException);
143143
if (result != null)

0 commit comments

Comments
 (0)