Esempio n. 1
0
        /// <summary>
        ///     异常返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnException(ExceptionContext context)
        {
            // 解析异常信息
            var(ErrorCode, ErrorObject) = UnifyContext.GetExceptionMetadata(context);

            return(new JsonResult(new TData <object>
            {
                Message = ErrorObject.ToString()
            }));
        }
Esempio n. 2
0
 /// <summary>
 /// 异常返回值
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public IActionResult OnException(ExceptionContext context)
 {
     // 解析异常信息
     var(StatusCode, Errors) = UnifyContext.GetExceptionMetadata(context);
     return(new JsonResult(new ApiResponse <object>
     {
         Code = StatusCode,
         Success = false,
         Data = null,
         Msg = Errors,
         Extras = UnifyContext.Take(),
         Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
     }));
 }
        /// <summary>
        /// 异常返回值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IActionResult OnException(ExceptionContext context)
        {
            // 解析异常信息
            var(StatusCode, ErrorCode, Errors) = UnifyContext.GetExceptionMetadata(context);

            return(new JsonResult(new RESTfulResult <object>
            {
                Code = StatusCode,
                Succeeded = false,
                Data = null,
                Msg = context.Exception is AppFriendlyException ? Errors : "系统异常,请联系管理员",//Errors
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// 异常拦截
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task OnExceptionAsync(ExceptionContext context)
        {
            // 解析异常处理服务,实现自定义异常额外操作,如记录日志等
            var globalExceptionHandler = context.HttpContext.RequestServices.GetService <IGlobalExceptionHandler>();

            if (globalExceptionHandler != null)
            {
                await globalExceptionHandler.OnExceptionAsync(context);
            }

            // 如果异常在其他地方被标记了处理,那么这里不再处理
            if (context.ExceptionHandled)
            {
                return;
            }

            // 获取控制器信息
            var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            // 解析异常信息
            var exceptionMetadata = UnifyContext.GetExceptionMetadata(context);

            // 判断是否是验证异常
            var isValidationException = context.Exception is AppFriendlyException friendlyException && friendlyException.ValidationException;

            // 判断是否跳过规范化结果,如果是,则只处理为友好异常消息
            if (UnifyContext.CheckFailedNonUnify(actionDescriptor.MethodInfo, out var unifyResult))
            {
                // 如果是验证异常,返回 400
                if (isValidationException)
                {
                    context.Result = new BadRequestResult();
                }
                else
                {
                    // 返回友好异常
                    context.Result = new ContentResult()
                    {
                        Content    = exceptionMetadata.Errors.ToString(),
                        StatusCode = exceptionMetadata.StatusCode
                    };
                }
            }
            else
            {
                // 判断是否支持 MVC 规范化处理
                if (!UnifyContext.CheckSupportMvcController(context.HttpContext, actionDescriptor, out _))
                {
                    return;
                }

                // 执行规范化异常处理
                context.Result = unifyResult.OnException(context, exceptionMetadata);
            }

            // 判断异常消息是否是验证异常(比如数据验证异常,业务抛出异常)
            if (isValidationException)
            {
                // 解析验证消息
                var validationMetadata = ValidatorContext.GetValidationMetadata((context.Exception as AppFriendlyException).ErrorMessage);

                App.PrintToMiniProfiler("Validation", "Failed", $"Validation Failed:\r\n{validationMetadata.Message}", true);
            }
            else
            {
                PrintToMiniProfiler(context.Exception);
            }
        }