Exemplo n.º 1
0
        /// <summary>
        /// Required method for a web api global exception handler
        /// </summary>
        public override Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            // The stock web api global handler throws an exception in this case;
            // duplicating that logic.
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // verify this exception should be handled at all; exit if not
            if (!ShouldHandle(context))
            {
                return(Task.FromResult(0));
            }

            // convert the exception into an RFC7807Exception if required, then create an IHttpActionResult from it
            var ex = context.Exception as RFC7807Exception;

            if (ex == null)
            {
                ex = new RFC7807Exception(context.Exception, context.Request.RequestUri);
            }

            context.Result = context.Request.CreateRFC7807ProblemActionResult(ex.ProblemDetail);

            return(Task.FromResult(0));
        }
        /// <summary>
        /// Create an HttpResponseMessage given an RFC7807ProblemDetail instance
        /// </summary>
        public static HttpResponseMessage CreateRFC7807ProblemResponse(this HttpRequestMessage request,
                                                                       RFC7807ProblemDetail detail)
        {
            var media     = RFC7807Media.GetRFC7807ContentTypeForRequest(request);
            var formatter = RFC7807Media.GetMediaTypeFormatterForResponseType(
                request.GetConfiguration(), media);

            var exception = new RFC7807Exception(detail);

            return(request.CreateResponse((HttpStatusCode)exception.ProblemDetail.Status, detail, formatter, media));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Required method for exception filters
        /// </summary>
        public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
        {
            var ex = context.Exception as RFC7807Exception;

            if (ex == null)
            {
                ex = new RFC7807Exception(context.Exception, context.Request.RequestUri);
            }

            context.Response = context.Request.CreateRFC7807ProblemResponse(ex.ProblemDetail);

            return(Task.FromResult(0));
        }
        /// <summary>
        /// Create an IHttpActionResult given an RFC7807ProblemDetail instance
        /// </summary>
        public static IHttpActionResult CreateRFC7807ProblemActionResult(this HttpRequestMessage request,
                                                                         RFC7807ProblemDetail detail)
        {
            var media     = RFC7807Media.GetRFC7807ContentTypeForRequest(request);
            var formatter = RFC7807Media.GetMediaTypeFormatterForResponseType(
                request.GetConfiguration(), media);

            var exception = new RFC7807Exception(detail);

            return(new FormattedContentResult <RFC7807ProblemDetail>(
                       (HttpStatusCode)exception.ProblemDetail.Status,
                       detail, formatter, new MediaTypeHeaderValue(media), request));
        }