Browse Source

fix: 添加DefaultHttpExceptionStatusCodeFinder

abp/9.3.0.4 9.3.0.5
zzzwangjun@gmail.com 7 months ago
parent
commit
59c953d33b
  1. 63
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs
  2. 4
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json

63
aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Volo/Abp/DefaultHttpExceptionStatusCodeFinder.cs

@ -0,0 +1,63 @@
namespace Volo.Abp;
/// <summary>
/// 修改Abp 返回状态码
/// 原因: 抛出BusinessException异常 不应该抛403异常
/// </summary>
public class DefaultHttpExceptionStatusCodeFinder : IHttpExceptionStatusCodeFinder, ITransientDependency
{
protected AbpExceptionHttpStatusCodeOptions Options { get; }
public DefaultHttpExceptionStatusCodeFinder(
IOptions<AbpExceptionHttpStatusCodeOptions> 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;
}
}

4
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",

Loading…
Cancel
Save