Last active
July 11, 2017 22:06
-
-
Save sloncho/f43e16f597548678b787 to your computer and use it in GitHub Desktop.
Inconsistent exception handling in NancyFx module pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Nancy; | |
using Nancy.Testing; | |
using NUnit.Framework; | |
using SharpTestsEx; | |
namespace StackOverflowExample.Nancy | |
{ | |
[TestFixture] | |
public sealed class ModuleOnError | |
{ | |
[Test] | |
public void Module_and_application_errorHooks_should_capture_same_exception() | |
{ | |
var errorModule = new ErrorModule(); | |
Exception applicationException = null; | |
var browser = new Browser(with => | |
{ | |
with.Module(errorModule); | |
with.ApplicationStartup( | |
(container, pipelines) => pipelines.OnError.AddItemToStartOfPipeline((context, exception) => | |
{ | |
applicationException = exception; | |
return Response.NoBody; | |
})); | |
}); | |
browser.Get("/"); | |
// This fails, as errorModule.ModuleException is unwrapped AggregateException | |
applicationException.Should().Be.SameInstanceAs(errorModule.ModuleException); | |
} | |
} | |
public sealed class ErrorModule : NancyModule | |
{ | |
public Exception ModuleException { get; private set; } | |
public ErrorModule() | |
{ | |
OnError.AddItemToStartOfPipeline((context, exception) => | |
{ | |
ModuleException = exception; | |
return null; | |
}); | |
Get["/"] = _ => | |
{ | |
throw new SomeException(); | |
}; | |
} | |
} | |
sealed class SomeException : Exception | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment