Created
January 23, 2015 11:22
-
-
Save jchannon/139a805ebfd525be30f9 to your computer and use it in GitHub Desktop.
This file contains 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
pipelines.OnError += (ctx, exception) => | |
{ | |
ctx.Items.Add("OnErrorException", exception); | |
return null; | |
}; |
This file contains 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
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