Last active
August 31, 2017 01:11
-
-
Save jchannon/6fe235343fe2e5c1d845 to your computer and use it in GitHub Desktop.
This will handle Nancy content negotiation on errors so the client receives JSON, XML or HTML based on the client Accept headers
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 CustomBootstrapper : DefaultNancyBootstrapper | |
{ | |
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) | |
{ | |
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 Error | |
{ | |
public string ErrorMessage { get; set; } | |
public string FullException { get; set; } | |
} |
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; | |
} | |
} |
@JeanTerblanche It may not be the same issue as you are seeing but I needed to call errorresponse.PreExecute(context);
to prevent a null reference exception within Nancy.Responses.MaterialisingResponse
.
L35 of errorstatuscodehandler.cs
becomes
var errorresponse = responseNegotiator.NegotiateResponse(response, context);
errorresponse.PreExecute(context);
context.Response = errorresponse;
@MacsDickinson thank you very much - I was encountering the same issue and this fixed it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is really great but it doesn't seem to work with Hosting.AspNet - any ideas?