Пример #1
0
        public void OnException(ExceptionContext context)
        {
            var exception = context.Exception;

            logger.LogError(
                new EventId(context.Exception.HResult),
                context.Exception,
                context.Exception.Message);

            if (context.Exception.GetType() == typeof(BusinessException) || context.Exception.GetType().BaseType == typeof(BusinessException))
            {
                // handle bussiness exception
                var json = new ApiErrorResult
                {
                    Success      = false,
                    ErrorCode    = context.Exception.GetType().Name,
                    ErrorMessage = context.Exception.Message,
                };

                // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
                //It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
                context.Result = new BadRequestObjectResult(json);
                context.WithNotification("error", string.Empty, json.ErrorMessage);

                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            else
            {
                // if it's not one of the expected exception, set it to 500
                var code = HttpStatusCode.InternalServerError;

                //TODO:Mapping if (exception is NotFoundExe) code = HttpStatusCode.NotFound;
                switch (exception)
                {
                case EntityNotFoundException _:
                    code = HttpStatusCode.NotFound;
                    break;

                case ArgumentNullException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case InvalidArgumentException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case HttpRequestException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case UnauthorizedAccessException _:
                    code = HttpStatusCode.Unauthorized;
                    break;
                }

                // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
                // It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
                context.Result = new ObjectResult(
                    new ApiErrorResult
                {
                    Success      = false,
                    ErrorCode    = code.ToString(),
                    ErrorMessage = exception.InnerException != null ? exception.InnerException.Message : exception.Message,
                    TechnicalLog = exception.GetExceptionTechnicalInfo(),
                });

                context.WithNotification("error", string.Empty, "Đã có lỗi xảy ra. Vui lòng thử lại sau.");

                context.HttpContext.Response.StatusCode = (int)code;
            }

            context.ExceptionHandled = true;
        }
Пример #2
0
        public void OnException(ExceptionContext context)
        {
            var exception = context.Exception;

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

            ApiErrorResult apiErrorResult;

            if (context.Exception.GetType() == typeof(BusinessException) || context.Exception.GetType().BaseType == typeof(BusinessException))
            {
                // handle bussiness exception
                apiErrorResult = new ApiErrorResult
                {
                    Success      = false,
                    ErrorCode    = context.Exception.GetType().Name,
                    ErrorMessage = context.Exception.Message,
                };

                context.Result = new BadRequestObjectResult(apiErrorResult);
                context.WithNotification("error", string.Empty, apiErrorResult.ErrorMessage);

                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            else
            {
                // if it's not one of the expected exception, set it to 500
                var code = HttpStatusCode.InternalServerError;

                //TODO:Mapping if (exception is NotFoundExe) code = HttpStatusCode.NotFound;
                switch (exception)
                {
                case EntityNotFoundException _:
                    code = HttpStatusCode.NotFound;
                    break;

                case ArgumentNullException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case InvalidArgumentException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case HttpRequestException _:
                    code = HttpStatusCode.BadRequest;
                    break;

                case UnauthorizedAccessException _:
                    code = HttpStatusCode.Unauthorized;
                    break;
                }

                apiErrorResult = new ApiErrorResult
                {
                    Success      = false,
                    ErrorCode    = code.ToString(),
                    ErrorMessage = exception.InnerException != null ? exception.InnerException.Message : exception.Message,
                    TechnicalLog = exception.GetExceptionTechnicalInfo(),
                };

                context.Result = new ObjectResult(apiErrorResult);

                context.WithNotification("error", string.Empty, "Đã có lỗi xảy ra. Vui lòng thử lại sau.");

                context.HttpContext.Response.StatusCode = (int)code;
            }


            context.ExceptionHandled = true;
        }