예제 #1
0
        public override void OnException(ExceptionContext context)
        {
            var urlHelper =
                (IUrlHelper)context.HttpContext.RequestServices.GetService(typeof(IUrlHelper));

            switch (context.Exception)
            {
            case PagingValidationException pagingValidationException:
                PageHateoasResponse pagingErrorResponse = ProcessPagingValidationException(pagingValidationException, context, this.stringResources.PageStringResources, urlHelper);

                context.Result = new BadRequestObjectResult(pagingErrorResponse);
                break;

            case ValidationException validationException:
                BaseHateoasResponse validationErrorResponse = ProcessValidationException(validationException);

                context.Result = new BadRequestObjectResult(validationErrorResponse);
                break;

            case SqlException _:
                BaseHateoasResponse sqlErrorResponse = ProcessSqlException(stringResources);

                context.Result = new BadRequestObjectResult(sqlErrorResponse);
                break;

            case Exception exception:
                BaseHateoasResponse errorResponse = ProcessException(exception);

                context.Result = new BadRequestObjectResult(errorResponse);
                break;
            }

            context.ExceptionHandled = true;
        }
예제 #2
0
        private static PageHateoasResponse ProcessPagingValidationException(PagingValidationException pagingValidationException, ExceptionContext context, PagingStringResources pageStringResources, IUrlHelper urlHelper)
        {
            const int DefaultPageNumber = 1;
            const int DefaultPageSize   = 25;

            var errorResponse = new PageHateoasResponse
            {
                Errors     = pagingValidationException.ValidationMessages,
                PageNumber = DefaultPageNumber,
                PageSize   = DefaultPageSize
            };

            var actionInfo = new ActionInfo
            {
                UrlHelper  = urlHelper,
                RouteName  = context.ActionDescriptor.AttributeRouteInfo.Name,
                HttpMethod = context.HttpContext.Request.Method
            };

            errorResponse.GenerateLinks(pageStringResources, actionInfo);
            return(errorResponse);
        }