I want my YSOD back!

I’ve recently been using Nancy to build an API, and have found it a pleasure to work with.

I do have one minor complaint though: it’s cutesy default error page…

Nancy default error page

I understand the motivation, and I can see how it helps with the “super-duper-happy-path”. But, when developing an API in a corporate environment, it’s frankly a bit embarrassing.

I want my YSOD back! The good news is, you can, with a little bit of work.

public class YsodErrorHandler : IErrorHandler
{
    public bool HandlesStatusCode(HttpStatusCode statusCode)
    {
        return statusCode == HttpStatusCode.InternalServerError;
    }

    public void Handle(HttpStatusCode statusCode, NancyContext context)
    {
        object errorObject;
        context.Items.TryGetValue(NancyEngine.ERROR_EXCEPTION, out errorObject);
        var exception = errorObject as Exception;

        if (exception != null)
        {
            throw exception;
        }
    }
}

Et voila!

YSOD

(Caveat: this will only work with 0.10.0, previous versions don’t provide the exception object). The best bit is, you don’t even have to register the error handler, Nancy will scan for it!

3 thoughts on “I want my YSOD back!

  1. Neil's avatar Neil December 19, 2012 / 5:40 pm

    Does unfortunately not work with latest version. ErrorObject is always null.

Leave a reply to Neil Cancel reply