private static Response CreateNegotiatedResponse(NancyContext context, IResponseNegotiator responseNegotiator, Exception exception, HttpServiceError defaultError) { HttpServiceError httpServiceError = ExtractFromException(exception, defaultError); Negotiator negotiator = new Negotiator(context) .WithServiceError(httpServiceError); return responseNegotiator.NegotiateResponse(negotiator, context); }
private static HttpServiceError ExtractFromException(Exception exception, HttpServiceError defaultValue) { HttpServiceError result = defaultValue; if (exception != null) { HttpServiceErrorException exceptionWithServiceError = exception as HttpServiceErrorException; if (exceptionWithServiceError != null) { result = exceptionWithServiceError.HttpServiceError; } } return result; }
public static void Enable(IPipelines pipelines, IResponseNegotiator responseNegotiator, HttpServiceError defaultError) { if (pipelines == null) { throw new ArgumentNullException("pipelines"); } if (responseNegotiator == null) { throw new ArgumentNullException("responseNegotiator"); } if (defaultError == null) { throw new ArgumentNullException("defaultError"); } pipelines.OnError += (context, exception) => HandleException(context, exception, responseNegotiator, defaultError); }
private static Response HandleException(NancyContext context, Exception exception, IResponseNegotiator responseNegotiator, HttpServiceError defaultError) { LogException(context, exception); return CreateNegotiatedResponse(context, responseNegotiator, exception, defaultError); }
public static Negotiator WithServiceError(this Negotiator negotiator, HttpServiceError httpServiceError) { return negotiator .WithStatusCode(httpServiceError.HttpStatusCode) .WithModel(httpServiceError.ServiceError); }