Esempio n. 1
0
        private IHttpActionResult GetExceptionActionResult(
            Exception exception,
            bool includeErrorDetail,
            IContentNegotiator contentNegotiator,
            HttpRequestMessage request,
            IEnumerable <MediaTypeFormatter> formatters)
        {
            // Add a ReferenceId property to the exception so we can log it and/or return it to the caller.
            exception.AddReferenceId();

            if (exception is NotAuthorizedException)
            {
                return(new StatusCodeResult(HttpStatusCode.Unauthorized, request));
            }

            if (exception is ForbiddenException)
            {
                return(new StatusCodeResult(HttpStatusCode.Forbidden, request));
            }

            var validationException = exception as ValidationException;

            if (validationException != null)
            {
                return(new ValidationExceptionResult(validationException, includeErrorDetail, contentNegotiator, request, formatters));
            }

            if (exception is DuplicateException)
            {
                return(new ConflictResult(request));
            }

            if (exception is NotFoundException)
            {
                return(new NotFoundResult(request));
            }

            _logger.Error(exception, "Received exception while processing request.\n{0}", request.RequestUri);

            var unavailableException = exception as ServiceUnavailableException;

            if (unavailableException != null)
            {
                return(new ServiceUnavailableExceptionResult(
                           unavailableException, includeErrorDetail, contentNegotiator, request, formatters));
            }

            return(new ReferencedExceptionResult(exception, includeErrorDetail, contentNegotiator, request, formatters));
        }