コード例 #1
0
        public void OnException(ExceptionContext context)
        {
            this._logger.LogError(new EventId(context.Exception.HResult),
                                  context.Exception,
                                  context.Exception.Message);

            if (context.Exception.GetType() == typeof(BasketDomainException))
            {
                var error = new JosnErrorResponse()
                {
                    Messages = new[] { context.Exception.Message }
                };

                context.Result = new BadRequestObjectResult(error);
                context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
            }
            else
            {
                var error = new JosnErrorResponse()
                {
                    Messages = new[] { context.Exception.Message }
                };

                if (this._env.IsDevelopment())
                {
                    error.DeveloperMessage = context.Exception;
                }

                context.Result = new InnerServerErrorObjectResult(error);
                context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
            }

            context.ExceptionHandled = true;
        }
コード例 #2
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.ModelState.IsValid)
            {
                return;
            }

            var validatedErrors = context.ModelState
                                  .Keys
                                  .SelectMany(key => context.ModelState[key].Errors)
                                  .Select(m => m.ErrorMessage);

            var error = new JosnErrorResponse()
            {
                Messages = validatedErrors.ToArray()
            };

            context.Result = new BadRequestObjectResult(error);
        }