Skip to content

Commit b7648f2

Browse files
committed
NancyEngine: try-catch around the CheckStatusCodeHandler and flatten
exception in the nancy engine extensions NancyFx#1458
1 parent e7bc45b commit b7648f2

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Nancy/NancyEngine.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ public Task<NancyContext> HandleRequest(Request request, Func<NancyContext, Nanc
101101
task.WhenCompleted(
102102
completeTask =>
103103
{
104-
this.CheckStatusCodeHandler(completeTask.Result);
104+
try
105+
{
106+
this.CheckStatusCodeHandler(completeTask.Result);
105107

106-
this.SaveTraceInformation(completeTask.Result);
108+
this.SaveTraceInformation(completeTask.Result);
109+
}
110+
catch (Exception ex)
111+
{
112+
tcs.SetException(ex);
113+
return;
114+
}
107115

108116
tcs.SetResult(completeTask.Result);
109117
},
@@ -280,7 +288,7 @@ private static void InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeli
280288
}
281289
}
282290

283-
private static Exception FlattenException(Exception exception)
291+
internal static Exception FlattenException(Exception exception)
284292
{
285293
if (exception is AggregateException)
286294
{

src/Nancy/NancyEngineExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ 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-
task.Wait();
32-
if (task.IsFaulted)
31+
try
3332
{
34-
throw task.Exception ?? new Exception("Request task faulted");
33+
task.Wait();
34+
}
35+
catch (Exception ex)
36+
{
37+
var flattenedException = NancyEngine.FlattenException(ex);
38+
throw flattenedException;
3539
}
3640
return task.Result;
3741
}

0 commit comments

Comments
 (0)