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!

Advertisements

3 thoughts on “I want my YSOD back!

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s