From 59c953d33ba3c38bf9a9e321aa67a90b7a8c783e Mon Sep 17 00:00:00 2001 From: "zzzwangjun@gmail.com" <510423039@qq.com> Date: Tue, 9 Sep 2025 20:38:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0DefaultHttpExceptionSt?= =?UTF-8?q?atusCodeFinder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultHttpExceptionStatusCodeFinder.cs | 63 +++++++++++++++++++ .../Lion.AbpPro.HttpApi.Host/appsettings.json | 4 +- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs new file mode 100644 index 00000000..d1fef81f --- /dev/null +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs @@ -0,0 +1,63 @@ +namespace Volo.Abp; + +/// +/// 修改Abp 返回状态码 +/// 原因: 抛出BusinessException异常 不应该抛403异常 +/// +public class DefaultHttpExceptionStatusCodeFinder : IHttpExceptionStatusCodeFinder, ITransientDependency +{ + protected AbpExceptionHttpStatusCodeOptions Options { get; } + + public DefaultHttpExceptionStatusCodeFinder( + IOptions options) + { + Options = options.Value; + } + + public HttpStatusCode GetStatusCode(HttpContext httpContext, Exception exception) + { + if (exception is IHasHttpStatusCode exceptionWithHttpStatusCode && + exceptionWithHttpStatusCode.HttpStatusCode > 0) + { + return (HttpStatusCode)exceptionWithHttpStatusCode.HttpStatusCode; + } + + if (exception is IHasErrorCode exceptionWithErrorCode && + !exceptionWithErrorCode.Code.IsNullOrWhiteSpace()) + { + if (Options.ErrorCodeToHttpStatusCodeMappings.TryGetValue(exceptionWithErrorCode.Code, out var status)) + { + return status; + } + } + + if (exception is AbpAuthorizationException) + { + return HttpStatusCode.Forbidden; + } + + //TODO: Handle SecurityException..? + + if (exception is AbpValidationException) + { + return HttpStatusCode.BadRequest; + } + + if (exception is EntityNotFoundException) + { + return HttpStatusCode.NotFound; + } + + if (exception is NotImplementedException) + { + return HttpStatusCode.NotImplemented; + } + + if (exception is IBusinessException) + { + return HttpStatusCode.InternalServerError; + } + + return HttpStatusCode.InternalServerError; + } +} \ No newline at end of file diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json index 656874b1..7ffad74a 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json @@ -44,7 +44,7 @@ "SelfUrl": "http://localhost:44315", }, "ConnectionStrings": { - "Default": "Data Source=localhost;Port=3306;Database=LionAbpProDemo9;uid=root;pwd=f616b8803ac7a9a0;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" + "Default": "Data Source=localhost;Port=3306;Database=LionAbpProDemo9;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" }, "Hangfire": { "Redis": { @@ -53,7 +53,7 @@ } }, "Redis": { - "Configuration": "localhost:6379,password=75He82bB5jFA84XZ1,defaultdatabase=2" + "Configuration": "localhost:6379,password=1q2w3E*,defaultdatabase=2" }, "Jwt": { "Audience": "Lion.AbpPro",