From 5e8b5eed9f11501512f7821aa5e79a247a5363a4 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 14 Aug 2024 16:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E5=AD=97=E5=85=B8=E7=9A=84=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E8=BF=9B=E8=A1=8C=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessFilter/ProjectExceptionFilter.cs | 24 ++++++++++++------- .../_IRaCIS/Output/ResponseOutput.cs | 12 +++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs index 1649f3ea7..823c168da 100644 --- a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs @@ -12,13 +12,13 @@ namespace IRaCIS.Core.Application.Filter { private readonly ILogger _logger; - public IStringLocalizer _localizer; + public IStringLocalizer _localizer; - public ProjectExceptionFilter(IStringLocalizer localizer, ILogger logger) + public ProjectExceptionFilter(IStringLocalizer localizer, ILogger logger) { _logger = logger; - _localizer = localizer; - } + _localizer = localizer; + } public void OnException(ExceptionContext context) { //context.ExceptionHandled;//记录当前这个异常是否已经被处理过了 @@ -27,7 +27,7 @@ namespace IRaCIS.Core.Application.Filter { if (context.Exception.GetType().Name == "DbUpdateConcurrencyException") { - //---并发更新,当前不允许该操作 + //---并发更新,当前不允许该操作 context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["ProjectException_ConcurrentUpdateNotAllowed"] + context.Exception.Message)); } @@ -35,14 +35,22 @@ namespace IRaCIS.Core.Application.Filter { var error = context.Exception as BusinessValidationFailedException; - context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message,"", error!.Code,localizedInfo: $"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}")); + var info = string.Empty; + + if (!string.IsNullOrWhiteSpace(error!.LocalizedKey) && StaticData.Log_Locoalize_Dic.ContainsKey(error!.LocalizedKey)) + { + info = $"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}"; + } + + + context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message, "", error!.Code, localizedInfo: info)); //warning 级别记录 //_logger.LogWarning($"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}"); } - else if(context.Exception.GetType() == typeof(QueryBusinessObjectNotExistException)) + else if (context.Exception.GetType() == typeof(QueryBusinessObjectNotExistException)) { - context.Result = new JsonResult(ResponseOutput.NotOk( context.Exception.Message, ApiResponseCodeEnum.DataNotExist)); + context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message, ApiResponseCodeEnum.DataNotExist)); } else { diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs index e0b46da46..9023c87e5 100644 --- a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace IRaCIS.Core.Infrastructure.Extention { @@ -112,7 +113,16 @@ namespace IRaCIS.Core.Infrastructure.Extention public static IResponseOutput NotOk(LocalizedString msg, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) { - return new ResponseOutput().NotOk(msg, code: code, localizedInfo: $"[{msg.Name}]:{StaticData.Log_Locoalize_Dic[msg.Name]}" ); + var key = msg.Name; + + var info = string.Empty; + + if (!string.IsNullOrWhiteSpace(key) && StaticData.Log_Locoalize_Dic.ContainsKey(key)) + { + info = $"[{key}]:{StaticData.Log_Locoalize_Dic[key]}"; + } + + return new ResponseOutput().NotOk(msg, code: code, localizedInfo: info); } public static IResponseOutput DBNotExistIfNUll(object businessObject)