Example #1
0
 /// <summary>
 ///  异常响应处理
 /// </summary>
 /// <param name="context"></param>
 /// <param name="res"></param>
 /// <param name="mode"></param>
 /// <returns></returns>
 private static async Task ExceptionResponse(HttpContext context, Resp res, AppSourceMode mode)
 {
     if (mode == AppSourceMode.Browser &&
         !AppWebInfoHelper.CheckIf404OrErrorUrl(context.Request.Path.ToString()))
     {
         var errUrl = res.IsRespType(RespTypes.ObjectNull) ? AppWebInfoHelper.NotFoundUrl : AppWebInfoHelper.ErrorUrl;
         if (!string.IsNullOrEmpty(errUrl))
         {
             string url = string.Concat(errUrl, "?ret=", res.ret, "&msg=", errUrl.UrlEncode());
             context.Response.Redirect(url);
             return;
         }
     }
     await ResponseJsonError(context.Response, res);
 }
Example #2
0
        /// <summary>
        ///  处理方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task Invoke(HttpContext context)
        {
            Exception     error;
            Resp          errorResp = null;
            AppSourceMode mode      = AppSourceMode.BrowserWithHeader;

            try
            {
                // 需要在此初始化,否则中间件依次退出后此值为空,下方异常无法捕获APP信息
                var appInfo = AppWebInfoHelper.GetOrSetAppIdentity(context);
                mode = appInfo.SourceMode;

                await _next.Invoke(context);

                if (context.Response.StatusCode == (int)HttpStatusCode.NotFound)
                {
                    await ExceptionResponse(context, new Resp(RespTypes.ObjectNull, "当前请求资源不存在!"), mode);
                }

                return;
            }
            catch (RespException resEx)
            {
                errorResp = new Resp().WithException(resEx);
                error     = resEx;
            }
            catch (Exception ex)
            {
                error = ex;
            }
            var code = LogHelper.Error(string.Concat("请求地址:", context.Request.Path, "错误信息:", error.Message, "详细信息:", error.StackTrace),
                                       nameof(ExceptionMiddleware));

#if DEBUG
            if (error != null)
            {
                throw error;
            }
#endif
            var res = errorResp ?? new Resp(RespTypes.InnerError, string.Concat("服务暂时不可用!详情错误码:", code));
            await ExceptionResponse(context, res, mode);
        }