Пример #1
0
        public static BadRequestResponse Create(string message)
        {
            var response = new BadRequestResponse();

            response.AddError(message);

            return(response);
        }
Пример #2
0
        public static BadRequestResponse CreateForNull()
        {
            var response = new BadRequestResponse();

            response.AddError("Content cannot be null");

            return(response);
        }
Пример #3
0
        public static BadRequestResponse Create(ModelStateDictionary modelState)
        {
            var response = new BadRequestResponse();

            response.PopulateModelErrors(modelState);

            return(response);
        }
Пример #4
0
        public override void Handle(ExceptionHandlerContext context)
        {
            if (context.Exception is ValidationFailedException)
            {
                var ex = context.Exception as ValidationFailedException;
                if (context.ExceptionContext.ActionContext != null)
                {
                    ModelStateDictionary modelState = context.ExceptionContext.ActionContext.ModelState;
                    modelState.AddValidationErrors(ex.ValidationErrors);

                    context.Result = new CustomHttpResult(
                        context.Request,
                        HttpStatusCode.BadRequest,
                        BadRequestResponse.Create(modelState));
                }
                else
                {
                    context.Result = new CustomHttpResult(
                        context.Request,
                        HttpStatusCode.BadRequest,
                        BadRequestResponse.Create(ex.ValidationErrors));
                }
            }
            else if (context.Exception is EntityNotFoundException)
            {
                context.Result = new CustomHttpResult(
                    context.Request,
                    HttpStatusCode.NotFound,
                    BaseApiResponse.NotFound(context.Request.RequestUri));
            }
            else if (context.Exception is ResourceForbiddenException)
            {
                context.Result = new CustomHttpResult(
                    context.Request,
                    HttpStatusCode.Forbidden,
                    BaseApiResponse.Forbidden());
            }
            else
            {
                context.Result = new CustomHttpResult(
                    context.Request,
                    context.Exception is SecurityException ? HttpStatusCode.Unauthorized : HttpStatusCode.InternalServerError,
                    BaseApiResponse.InternalServerError(context.Exception.Message));
            }
        }
Пример #5
0
        public static BadRequestResponse Create(IEnumerable <string> validationErrors)
        {
            var             response = new BadRequestResponse();
            BadRequestError badRequestError;

            if (!response.ModelErrors.TryGetValue(string.Empty, out badRequestError))
            {
                badRequestError = new BadRequestError();
                response.ModelErrors.Add(string.Empty, badRequestError);
            }

            foreach (string err in validationErrors)
            {
                badRequestError.AddError(err);
            }

            return(response);
        }
Пример #6
0
 protected IHttpActionResult BadRequest(IEnumerable <string> errors)
 {
     return(new CustomHttpResult(Request, HttpStatusCode.BadRequest, BadRequestResponse.Create(errors)));
 }
Пример #7
0
 protected new IHttpActionResult BadRequest(string message)
 {
     return(new CustomHttpResult(Request, HttpStatusCode.BadRequest, BadRequestResponse.Create(message)));
 }
Пример #8
0
 protected new IHttpActionResult BadRequest()
 {
     return(new CustomHttpResult(Request, HttpStatusCode.BadRequest, BadRequestResponse.Create(ModelState)));
 }