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; }
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); }