Esempio n. 1
0
        public static BaseApiResponse OperationImpossible(string message = null)
        {
            var response = new BaseApiResponse()
            {
                Message = "Operation is impossible",
            };

            if (message != null)
            {
                response.AddError(message);
            }

            return(response);
        }
Esempio n. 2
0
        public static BaseApiResponse InternalServerError(string message = null)
        {
            var response = new BaseApiResponse()
            {
                Message = "Internal server error"
            };

            if (message != null)
            {
                response.AddError(message);
            }

            return(response);
        }
Esempio n. 3
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));
            }
        }
Esempio n. 4
0
 protected IHttpActionResult Conflict(string message)
 {
     return(new CustomHttpResult(Request, HttpStatusCode.Conflict, BaseApiResponse.OperationImpossible(message)));
 }
Esempio n. 5
0
 protected new IHttpActionResult NotFound()
 {
     return(new CustomHttpResult(Request, HttpStatusCode.NotFound, BaseApiResponse.NotFound(Request.RequestUri)));
 }
Esempio n. 6
0
 protected IHttpActionResult InternalServerError(BaseApiResponse response)
 {
     return(new CustomHttpResult(Request, HttpStatusCode.InternalServerError, response));
 }