Skip to content

Commit 8441e98

Browse files
committed
Merge pull request NancyFx#463 from grumpydev/DisableErrorHandlingInConfigurableBootstrapper
Fixed NancyFx#458 - the default 500 error page is replaced by a real exception
2 parents 696ff99 + add65f9 commit 8441e98

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

Diff for: src/Nancy.Testing.Tests/ConfigurableBootstrapperFixture.cs

+26
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ public void Should_provide_configuration_for_all_base_properties()
109109
}
110110
}
111111

112+
[Fact]
113+
public void Should_throw_exceptions_if_any_occur_in_route()
114+
{
115+
var bootstrapper = new ConfigurableBootstrapper(with =>
116+
{
117+
with.Module<BlowUpModule>();
118+
});
119+
bootstrapper.Initialise();
120+
var engine = bootstrapper.GetEngine();
121+
var request = new Request("GET", "/", "http");
122+
123+
var result = Record.Exception(() => engine.HandleRequest(request));
124+
125+
result.ShouldNotBeNull();
126+
result.ShouldBeOfType<Exception>();
127+
result.ToString().ShouldContain("Oh noes!");
128+
}
129+
112130
public IEnumerable<string> GetConfigurableBootstrapperMembers()
113131
{
114132
var ignoreList = new[]
@@ -146,5 +164,13 @@ public void HandleRequest(Request request, Action<NancyContext> onComplete, Acti
146164
throw new NotImplementedException();
147165
}
148166
}
167+
168+
private class BlowUpModule : NancyModule
169+
{
170+
public BlowUpModule()
171+
{
172+
Get["/"] = _ => { throw new InvalidOperationException("Oh noes!"); };
173+
}
174+
}
149175
}
150176
}

Diff for: src/Nancy.Testing/ConfigurableBootstrapper.cs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public ConfigurableBootstrapper(Action<ConfigurableBoostrapperConfigurator> conf
4848
var configurator =
4949
new ConfigurableBoostrapperConfigurator(this);
5050

51+
configurator.ErrorHandler<PassThroughErrorHandler>();
52+
5153
configuration.Invoke(configurator);
5254
}
5355
}

Diff for: src/Nancy.Testing/Nancy.Testing.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -113,6 +113,7 @@
113113
<Compile Include="IBrowserContextValues.cs" />
114114
<Compile Include="IndexHelper.cs" />
115115
<Compile Include="NodeWrapper.cs" />
116+
<Compile Include="PassThroughErrorHandler.cs" />
116117
<Compile Include="PathHelper.cs" />
117118
<Compile Include="QueryWrapper.cs" />
118119
<Compile Include="AssertExtensions.cs" />

Diff for: src/Nancy.Testing/PassThroughErrorHandler.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Nancy.ErrorHandling;
3+
4+
namespace Nancy.Testing
5+
{
6+
public class PassThroughErrorHandler : IErrorHandler
7+
{
8+
public bool HandlesStatusCode(HttpStatusCode statusCode)
9+
{
10+
return statusCode == HttpStatusCode.InternalServerError;
11+
}
12+
13+
public void Handle(HttpStatusCode statusCode, NancyContext context)
14+
{
15+
throw new Exception("ConfigurableBootstrapper Exception", context.Items[NancyEngine.ERROR_EXCEPTION] as Exception);
16+
}
17+
}
18+
}

Diff for: src/Nancy/NancyEngine.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
/// </summary>
1414
public class NancyEngine : INancyEngine
1515
{
16-
internal const string ERROR_KEY = "ERROR_TRACE";
17-
internal const string ERROR_EXCEPTION = "ERROR_EXCEPTION";
16+
public const string ERROR_KEY = "ERROR_TRACE";
17+
public const string ERROR_EXCEPTION = "ERROR_EXCEPTION";
1818

1919
private readonly IRouteResolver resolver;
2020
private readonly IRouteCache routeCache;

0 commit comments

Comments
 (0)