From 767513802c11553fe15d8e2704c848623fae1ff5 Mon Sep 17 00:00:00 2001 From: mahao Date: Thu, 31 Aug 2023 14:42:52 +0800 Subject: [PATCH] up --- .../SettleAccountHttpApiHostModule.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/SettleAccountHttpApiHostModule.cs b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/SettleAccountHttpApiHostModule.cs index bf0c7668..42508c2d 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/SettleAccountHttpApiHostModule.cs +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/SettleAccountHttpApiHostModule.cs @@ -7,9 +7,11 @@ using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using StackExchange.Redis; using Swashbuckle.AspNetCore.SwaggerUI; @@ -20,6 +22,7 @@ using System.Text.Json; using Volo.Abp; using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.ExceptionHandling; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; @@ -258,6 +261,17 @@ namespace Win.Sfs.SettleAccount { //将错误明细发送给客户端 Configure(options => { options.SendExceptionsDetailsToClients = true; }); + + Configure(options => + { + var index = options.Filters.ToList().FindIndex(filter => filter is ServiceFilterAttribute attr && attr.ServiceType.Equals(typeof(AbpExceptionFilter))); + if (index > -1) + { + options.Filters.RemoveAt(index); + } + options.Filters.Add(typeof(CustomExceptionFilter)); + }); + } private void ConfigureMultiTenancy() @@ -476,4 +490,21 @@ namespace Win.Sfs.SettleAccount }); } } + + public class CustomExceptionFilter : IExceptionFilter + { + private readonly ILogger logger; + + public CustomExceptionFilter(ILogger logger) + { + this.logger = logger; + } + public void OnException(ExceptionContext context) + { + logger.LogError(new EventId(context.Exception.HResult), context.Exception, context.Exception.Message); + var message = context.Exception.InnerException?.InnerException?.Message ?? context.Exception.Message; + context.Result = new JsonResult(new { code = 400, message }); + context.ExceptionHandled = true; + } + } }