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

Fix: Exception during content rendering kills the self host process #563

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs
Expand Up @@ -131,6 +131,7 @@ private static NancyHostWrapper CreateAndOpenSelfHost(INancyBootstrapper nancyBo
nancyBootstrapper, nancyBootstrapper,
BaseUri); BaseUri);



try try
{ {
host.Start(); host.Start();
Expand All @@ -143,6 +144,23 @@ private static NancyHostWrapper CreateAndOpenSelfHost(INancyBootstrapper nancyBo
return new NancyHostWrapper(host); return new NancyHostWrapper(host);
} }



[SkippableFact]
public void Should_be_able_to_recover_from_rendering_exception()
{
using (CreateAndOpenSelfHost())
{

var reader =
new StreamReader(WebRequest.Create(new Uri(BaseUri,"exception")).GetResponse().GetResponseStream());

var response = reader.ReadToEnd();

response.ShouldEqual("Content");
}
}


private class NancyHostWrapper : IDisposable private class NancyHostWrapper : IDisposable
{ {
private readonly NancyHost host; private readonly NancyHost host;
Expand Down
12 changes: 11 additions & 1 deletion src/Nancy.Hosting.Self.Tests/TestModule.cs
@@ -1,4 +1,6 @@
namespace Nancy.Hosting.Self.Tests using System;

namespace Nancy.Hosting.Self.Tests
{ {
using System.IO; using System.IO;


Expand All @@ -19,6 +21,14 @@ public TestModule()
}; };


Post["/rel"] = parameters => new StreamReader(Request.Body).ReadToEnd(); Post["/rel"] = parameters => new StreamReader(Request.Body).ReadToEnd();

Get["/exception"] = parameters => new Response() {Contents = s =>
{
var writer = new StreamWriter(s);
writer.Write("Content");
writer.Flush();
throw new Exception("An error occured during content rendering");
}};
} }
} }
} }
24 changes: 20 additions & 4 deletions src/Nancy.Hosting.Self/NancyHost.cs
Expand Up @@ -184,12 +184,28 @@ private void GotCallback(IAsyncResult ar)


private void Process(HttpListenerContext ctx) private void Process(HttpListenerContext ctx)
{ {
var nancyRequest = try
ConvertRequestToNancyRequest(ctx.Request); {


using (var nancyContext = engine.HandleRequest(nancyRequest)) var nancyRequest = ConvertRequestToNancyRequest(ctx.Request);
using (var nancyContext = engine.HandleRequest(nancyRequest))
{

try
{
ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response);
}
catch (Exception ex)
{
nancyContext.Trace.TraceLog.WriteLog(s => s.AppendLine(string.Concat("[SelfHost] Exception while rendering response: ", ex)));
//TODO - the content of the tracelog is not used in this case
}
}
}
catch (Exception)
{ {
ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response); //TODO - this swallows the exception so that it doesn't kill the host
// pass it to the host process for handling by the caller ?
} }
} }
} }
Expand Down