示例#1
0
        public override BadRequestObjectResult BadRequest([ActionResultObjectValue] ModelStateDictionary modelState)
        {
            var problemDetails = new ServiceBadRequest(ModelState)
            {
                Status = StatusCodes.Status400BadRequest,
            };

            return(base.BadRequest(problemDetails));
        }
示例#2
0
        public static ProblemDetails GetProblemDetails(this Exception ex)
        {
            var problem = new ServiceBadRequest();

            problem.Detail = ex.Message;
            problem.Title  = string.Concat("Unknown Exception", ex.Message);
            problem.Status = 400;

            if (ex is UnauthorizedOperationException || ex is UnauthorizedAccessException)
            {
                problem.Title  = string.Concat("Unauthorized Access.", ex.Message);
                problem.Status = 403;
            }
            if (ex is EntityOperationException)
            {
                problem.Title  = string.Concat("Entity Operation Exception.", ex.Message);
                problem.Status = 400;
            }
            if (ex is EntityValidationException)
            {
                problem        = GetValidationProblemDetails(ex as EntityValidationException);
                problem.Title  = string.Concat("Validation Error.", ex.Message);
                problem.Status = 422;
            }
            if (ex is EntityAlreadyExistsException)
            {
                problem.Title  = string.Concat("Entity Already Exist.", ex.Message);
                problem.Status = 409;
            }
            if (ex is EntityConcurrencyException)
            {
                problem.Title  = string.Concat("Entity Concurrency Error.", ex.Message);
                problem.Status = 409;
            }
            if (ex is EntityDoesNotExistException)
            {
                problem.Title         = string.Concat("Entity Does Not Exist.", ex.Message);
                problem.Status        = 400;
                problem.serviceErrors = new string[1] {
                    "Record(s) no longer exists or you do not have authorized access."
                };
            }
            if (ex is CannotInsertDuplicateConstraintException)
            {
                Dictionary <string, string[]>            errors = new Dictionary <string, string[]>();
                CannotInsertDuplicateConstraintException cidce  = ex as CannotInsertDuplicateConstraintException;
                List <string> messages = new List <string>();
                messages.Add($"{cidce.EntityName} {cidce.PropertyName} already exists. Cannot add duplicate.");
                errors.Add(cidce.PropertyName, messages.ToArray());
                problem        = new ServiceBadRequest(errors);
                problem.Title  = string.Concat("Cannot Insert Duplicate Constraint.", ex.Message);
                problem.Status = 400;
            }
            return(problem);
        }
        public static ServiceBadRequest GetValidationProblemDetails(EntityValidationException ex)
        {
            Dictionary <string, string[]> errors = new Dictionary <string, string[]>();
            var results = ex.Validation;

            var members = results.SelectMany(r => r.MemberNames);

            foreach (var member in members)
            {
                List <string> errMessages = new List <string>();
                foreach (var error in results.Where(r => r.MemberNames.Contains(member)))
                {
                    errMessages.Add(error.ErrorMessage);
                }
                errors.Add(member, errMessages.ToArray());
            }
            ServiceBadRequest details = new ServiceBadRequest(errors);

            return(details);
        }
        public static ProblemDetails GetProblemDetails(this Exception ex)
        {
            var problem = new ServiceBadRequest();

            problem.Detail        = ex.Message;
            problem.Title         = string.Concat("Unknown Exception. ", ex.Message);
            problem.Status        = 400;
            problem.serviceErrors = new string[1] {
                ex.Message
            };

            if (ex is UnauthorizedOperationException || ex is UnauthorizedAccessException)
            {
                problem.Title  = string.Concat("Unauthorized Access. ", ex.Message);
                problem.Status = 403;
            }
            if (ex is EntityOperationException)
            {
                problem.Title  = string.Concat("Entity Operation Exception. ", ex.Message);
                problem.Status = 400;
            }
            if (ex is EntityValidationException)
            {
                problem        = GetValidationProblemDetails(ex as EntityValidationException);
                problem.Title  = string.Concat("Validation Error. ", ex.Message);
                problem.Status = 422;
            }
            if (ex is EntityAlreadyExistsException)
            {
                problem.Title  = string.Concat("Entity Already Exists. ", ex.Message);
                problem.Status = 409;
            }
            if (ex is EntityConcurrencyException)
            {
                problem.Title         = string.Concat("Entity Concurrency Error.", ex.Message);
                problem.Status        = 409;
                problem.serviceErrors = new string[1] {
                    "Record is being updated by another user, please try again."
                };
            }
            if (ex is EntityDoesNotExistException)
            {
                problem.Title         = string.Concat("Entity Does Not Exist. ", ex.Message);
                problem.Status        = 400;
                problem.serviceErrors = new string[1] {
                    "Record(s) no longer exists or you do not have authorized access."
                };
            }
            if (ex is ServerBusyException || ex.Message.ToLower().Contains("too many requests"))
            {
                problem.Title         = string.Concat("Server Busy, Too Many Requests.", ex.Message);
                problem.Status        = 429;
                problem.serviceErrors = new string[1] {
                    "Server is busy, please try again latter."
                };
            }
            if (ex is CannotInsertDuplicateConstraintException)
            {
                Dictionary <string, string[]>            errors = new Dictionary <string, string[]>();
                CannotInsertDuplicateConstraintException cidce  = ex as CannotInsertDuplicateConstraintException;
                List <string> messages = new List <string>();
                messages.Add($"{cidce.EntityName} {cidce.PropertyName} already exists. Cannot add duplicate.");
                errors.Add(cidce.PropertyName, messages.ToArray());
                problem        = new ServiceBadRequest(errors);
                problem.Title  = string.Concat("Cannot Insert Duplicate Constraint.", ex.Message);
                problem.Status = 400;
            }
            if (ex is TimeoutException)
            {
                problem.Title  = string.Concat("Request Timed Out.", ex.Message);
                problem.Status = 400;
            }
            return(problem);
        }