Exemplo n.º 1
0
 /// <summary>
 /// 返回成功消息
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="subCode"></param>
 /// <param name="message"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static Result <T> Success <T>(SubCode subCode, string message, T data = default)
 {
     (var code, _) = SubCodeMessage.Message(subCode);
     return(new Result <T>()
     {
         code = 0,
         subCode = code,
         message = message,
         data = data,
         elapsedTime = -1,
         operationTime = DateTime.Now
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="context">异常上下文</param>
        public override void OnException(ExceptionContext context)
        {
            if (context == null)
            {
                return;
            }

            if (context.Exception is OperationCanceledException)
            {
                context.ExceptionHandled = true;
                context.Result           = new ObjectResult(new Result <object>(StateCode.Fail, R.CanceledMessage))
                {
                    StatusCode = (int)HttpStatusCode.BadRequest
                };
            }
            else if (context.Exception is UnauthorizedAccessException)
            {
                context.Result = new ObjectResult(new Result <object>(StateCode.Fail, context.Exception.Message))
                {
                    StatusCode = (int)HttpStatusCode.Unauthorized
                };
            }
            else if (context.Exception is ValidationException)
            {
                var ex = context.Exception as ValidationException;

                var message = ex.Failures.Select(c => c.Value.Join(",")).Join("");

                (var code, _) = SubCodeMessage.Message(SubCode.Fail);

                context.Result = new ObjectResult(new Result <object>()
                {
                    code          = 0,
                    subCode       = code,
                    message       = message,
                    data          = null,
                    elapsedTime   = -1,
                    operationTime = DateTime.Now
                })
                {
                    StatusCode = (int)HttpStatusCode.OK
                };
            }
            else
            {
                var logger = context.HttpContext.RequestServices.GetService <ILogger <ApiErrorAttribute> >();

                if (logger.IsEnabled(LogLevel.Error))
                {
                    var routes = context.GetRouteValues().Select(c => $"{c.Key}={c.Value}").Join(",");

                    var str = new Str();
                    str.AppendLine("WebApi全局异常");
                    str.AppendLine($"路由信息:{routes}");
                    str.AppendLine($"IP:{context.HttpContext.Connection.RemoteIpAddress}");
                    str.AppendLine($"请求方法:{context.HttpContext.Request.Method}");
                    str.AppendLine($"请求地址:{context.HttpContext.Request.Scheme}://{context.HttpContext.Request.Host}{context.HttpContext.Request.Path}{context.HttpContext.Request.QueryString}");
                    logger.LogError(context.Exception.FormatMessage(str.ToString()));
                }

                context.Result = new ObjectResult(new Result <object>(StateCode.Fail, context.Exception.Message))
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError
                };

                base.OnException(context);
            }
        }