Skip to content

Instantly share code, notes, and snippets.

@jchannon
Created January 23, 2015 11:22
Show Gist options
  • Save jchannon/139a805ebfd525be30f9 to your computer and use it in GitHub Desktop.
Save jchannon/139a805ebfd525be30f9 to your computer and use it in GitHub Desktop.
pipelines.OnError += (ctx, exception) =>
{
ctx.Items.Add("OnErrorException", exception);
return null;
};
public class ErrorStatusCodeHandler : IStatusCodeHandler
{
private readonly IResponseNegotiator responseNegotiator;
public ErrorStatusCodeHandler(IResponseNegotiator responseNegotiator)
{
this.responseNegotiator = responseNegotiator;
}
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.InternalServerError;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
var response = new Negotiator(context);
Error error = null;
if (context.Items.ContainsKey("OnErrorException"))
{
var exception = context.Items["OnErrorException"] as Exception;
error = new Error { ErrorMessage = exception.Message, FullException = exception.ToString() };
}
response.WithModel(new ErrorPageViewModel
{
Title = "Sorry, something went wrong",
Summary = error == null ? "An unexpected error occurred." : error.ErrorMessage,
Details = error == null ? null : error.FullException
}).WithStatusCode(statusCode).WithView("Error");
var errorresponse = responseNegotiator.NegotiateResponse(response, context);
context.Response = errorresponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment