Пример #1
0
        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);
        }
Пример #2
0
        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;
        }
Пример #3
0
        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);
        }
Пример #4
0
        private static Response HandleException(NancyContext context, Exception exception, IResponseNegotiator responseNegotiator, HttpServiceError defaultError)
        {
            LogException(context, exception);

            return CreateNegotiatedResponse(context, responseNegotiator, exception, defaultError);
        }
Пример #5
0
 public static Negotiator WithServiceError(this Negotiator negotiator, HttpServiceError httpServiceError)
 {
     return negotiator
         .WithStatusCode(httpServiceError.HttpStatusCode)
         .WithModel(httpServiceError.ServiceError);
 }