Пример #1
0
        public void OnException(ExceptionContext context)
        {
            if (context.Exception is ApiDomainException domainException)
            {
                //var aggregateMessage = domainException.Messages == null
                //    ? context.Exception.Message
                //    : JsonConvert.SerializeObject(domainException.Messages);

                //_logger.LogError(new EventId(context.Exception.HResult),
                //    context.Exception,
                //    aggregateMessage);

                var result = ApiErrorResult.BadRequest(domainException.Messages ?? new[] { context.Exception.Message });

                context.Result = new BadRequestObjectResult(result);
                context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
            }
            else
            {
                _logger.LogError(new EventId(context.Exception.HResult),
                                 context.Exception,
                                 context.Exception.Message);

                object devMessage = null;
                if (_env.IsDevelopment())
                {
                    devMessage = LogFormatter.PrintException(context.Exception);
                }

                var result = ApiExceptionResult.InternalServerError(new[] { "An server error occur." }, devMessage);

                context.Result = new InternalServerErrorObjectResult(result);
                context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
            }

            context.ExceptionHandled = true;
        }
Пример #2
0
        public InternalServerErrorObjectResult ServerError(int status = StatusCodes.Status500InternalServerError, params string[] messages)
        {
            var result = ApiExceptionResult.InternalServerError(messages);

            return(new InternalServerErrorObjectResult(result));
        }