Exemplo n.º 1
0
        /// <summary> 异常处理 </summary>
        /// <param name="exception"></param>
        /// <param name="requestBody"></param>
        public static DResult Handler(Exception exception, string requestBody = null)
        {
            DResult result = null;
            var     ex     = exception.GetBaseException();

            switch (ex)
            {
            case BusiException busi:
                OnBusiException?.Invoke(busi);
                result = DResult.Error(busi.Message, busi.Code);
                break;

            case OperationCanceledException _:
                //操作取消
                break;

            default:
                OnException?.Invoke(ex);
                var msg = new LogErrorMsg
                {
                    Message = ex.Message
                };
                if (AcbHttpContext.Current != null)
                {
                    msg.Url = AcbHttpContext.RawUrl;
                    if (string.IsNullOrWhiteSpace(requestBody))
                    {
                        var input = AcbHttpContext.Body;
                        if (input.CanSeek)
                        {
                            input.Seek(0, SeekOrigin.Begin);
                        }
                        if (input.CanRead)
                        {
                            using (var stream = new StreamReader(input))
                            {
                                msg.Form = stream.ReadToEnd();
                            }
                        }
                    }
                    else
                    {
                        msg.Form = requestBody;
                    }
                }

                Logger.Error(msg.ToString(), ex);
                result = ErrorCodes.SystemError.CodeResult();
                break;
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary> 异常处理 </summary>
        /// <param name="exception"></param>
        /// <param name="requestBody"></param>
        public static async Task <DResult> HandlerAsync(Exception exception, string requestBody = null)
        {
            DResult result = null;
            var     ex     = exception.GetBaseException();

            switch (ex)
            {
            case BusiException busi:
                OnBusiException?.Invoke(busi);
                result = DResult.Error(busi.Message, busi.Code);
                break;

            case OperationCanceledException _:
            case SocketException se when se.Message == "Operation canceled":
                //操作取消
                break;

            default:
                OnException?.Invoke(ex);
                var msg = new LogErrorMsg
                {
                    Message = ex.Message
                };
                if (CurrentIocManager.Context != null)
                {
                    var context = CurrentIocManager.Context;
                    var wrap    = CurrentIocManager.ContextWrap;
                    msg.Url       = context.RawUrl();
                    msg.ClientIp  = wrap.ClientIp;
                    msg.UserAgent = wrap.UserAgent;
                    msg.Form      = string.IsNullOrWhiteSpace(requestBody)
                            ? await wrap.FromBody <string>()
                            : requestBody;

                    msg.Token = wrap.Authorization ?? string.Empty;
                }
                Logger.LogError(ex, JsonHelper.ToJson(msg));
                result = ErrorCodes.SystemError.CodeResult();
                break;
            }

            return(result);
        }