/// <summary> /// Handle the code that comes in and display the correct page. /// </summary> /// <param name="statusCode">The status code.</param> /// <param name="context">The <see cref="NancyContext"/> that the code rides on.</param> public void Handle(HttpStatusCode statusCode, NancyContext context) { try { var jsendObject = new JSendObject { Status = StatusMessage.fail, StatusCode = statusCode, Route = context.Request.Url, Method = context.Request.Method, Data = statusCode.ToString() }; context.NegotiationContext = new NegotiationContext(); var negotiator = new Negotiator(context) .WithStatusCode(statusCode) .WithModel(jsendObject) .WithAllowedMediaRange("application/json") .WithAllowedMediaRange("text/html"); context.Response = this.responseNegotiator.NegotiateResponse(negotiator, context); } catch (Exception) { RemoveCode((int)statusCode); context.Response.StatusCode = statusCode; } }
/// <summary> /// Constructs the fail response. /// </summary> /// <param name="negotiator"> /// The <see cref="Negotiator"/>. /// </param> /// <param name="wrapperName"> /// Name of the wrapper. /// </param> /// <param name="packageMessage"> /// The package message. /// </param> /// <param name="context"> /// The request context. /// </param> /// <param name="code"> /// The status code. /// </param> /// <param name="e"> /// The optional exception used for logging. /// </param> /// <returns> /// The complete JSON response. /// </returns> public static Negotiator ConstructFailResponse(Negotiator negotiator, string wrapperName, string packageMessage, NancyContext context, HttpStatusCode code = HttpStatusCode.NotFound, Exception e = null) { if (e != null) { Logger.Error(e, "Fail response initiated."); } var wrapper = new Dictionary <string, string> { { wrapperName, packageMessage } }; var jsendObject = new JSendObject { Status = StatusMessage.fail, StatusCode = code, Route = context.Request.Url, Method = context.Request.Method, Data = wrapper }; return(negotiator .WithModel(jsendObject) .WithAllowedMediaRange("application/json") .WithStatusCode(code) .WithAllowedMediaRange("text/html")); }
/// <summary> /// Constructs the success response. /// </summary> /// <param name="negotiator">The <see cref="Negotiator"/>.</param> /// <param name="container">The response container.</param> /// <param name="context">The context of the request.</param> /// <param name="statusCode">The status code to provide with this response. 200 OK is default and this is usually perfectly fine, but more specific codes can be provided.</param> /// <returns>The complete JSON response</returns> public static Negotiator ConstructSuccessResponse(Negotiator negotiator, ResponseContainer container, NancyContext context, HttpStatusCode statusCode = HttpStatusCode.OK) { var jsendObject = new JSendObject { Status = StatusMessage.success, StatusCode = statusCode, Route = context.Request.Url, Method = context.Request.Method, Data = container }; return(negotiator .WithModel(jsendObject) .WithAllowedMediaRange("application/json") .WithAllowedMediaRange("text/html") .WithStatusCode(statusCode)); }