取字典的值的时候进行判断
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-08-14 16:52:03 +08:00
parent 682e40febd
commit 5e8b5eed9f
2 changed files with 27 additions and 9 deletions

View File

@ -12,13 +12,13 @@ namespace IRaCIS.Core.Application.Filter
{ {
private readonly ILogger<ProjectExceptionFilter> _logger; private readonly ILogger<ProjectExceptionFilter> _logger;
public IStringLocalizer _localizer; public IStringLocalizer _localizer;
public ProjectExceptionFilter(IStringLocalizer localizer, ILogger<ProjectExceptionFilter> logger) public ProjectExceptionFilter(IStringLocalizer localizer, ILogger<ProjectExceptionFilter> logger)
{ {
_logger = logger; _logger = logger;
_localizer = localizer; _localizer = localizer;
} }
public void OnException(ExceptionContext context) public void OnException(ExceptionContext context)
{ {
//context.ExceptionHandled;//记录当前这个异常是否已经被处理过了 //context.ExceptionHandled;//记录当前这个异常是否已经被处理过了
@ -27,7 +27,7 @@ namespace IRaCIS.Core.Application.Filter
{ {
if (context.Exception.GetType().Name == "DbUpdateConcurrencyException") if (context.Exception.GetType().Name == "DbUpdateConcurrencyException")
{ {
//---并发更新,当前不允许该操作 //---并发更新,当前不允许该操作
context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["ProjectException_ConcurrentUpdateNotAllowed"] + context.Exception.Message)); 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; 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 级别记录 //warning 级别记录
//_logger.LogWarning($"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}"); //_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 else
{ {

View File

@ -2,6 +2,7 @@
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace IRaCIS.Core.Infrastructure.Extention namespace IRaCIS.Core.Infrastructure.Extention
{ {
@ -112,7 +113,16 @@ namespace IRaCIS.Core.Infrastructure.Extention
public static IResponseOutput<string> NotOk(LocalizedString msg, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) public static IResponseOutput<string> NotOk(LocalizedString msg, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed)
{ {
return new ResponseOutput<string>().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<string>().NotOk(msg, code: code, localizedInfo: info);
} }
public static IResponseOutput<string> DBNotExistIfNUll(object businessObject) public static IResponseOutput<string> DBNotExistIfNUll(object businessObject)