Skip to content

Instantly share code, notes, and snippets.

@sloncho
Last active July 11, 2017 22:06
Show Gist options
  • Save sloncho/f43e16f597548678b787 to your computer and use it in GitHub Desktop.
Save sloncho/f43e16f597548678b787 to your computer and use it in GitHub Desktop.
Inconsistent exception handling in NancyFx module pipeline
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