feat: 实现字典管理后端
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
@@ -7,6 +9,8 @@ using System.Collections.Generic;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using FluentValidationException = FluentValidation.ValidationException;
|
||||
using SharedValidationException = TakeoutSaaS.Shared.Abstractions.Exceptions.ValidationException;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Web.Middleware;
|
||||
|
||||
@@ -79,9 +83,27 @@ public sealed class ExceptionHandlingMiddleware(RequestDelegate next, ILogger<Ex
|
||||
{
|
||||
return exception switch
|
||||
{
|
||||
ValidationException validationException => (
|
||||
DbUpdateConcurrencyException => (
|
||||
StatusCodes.Status409Conflict,
|
||||
ApiResponse<object>.Error(
|
||||
ErrorCodes.Conflict,
|
||||
"数据已被他人修改,请刷新后重试",
|
||||
new Dictionary<string, string[]>
|
||||
{
|
||||
["RowVersion"] = ["数据已被他人修改,请刷新后重试"]
|
||||
})),
|
||||
UnauthorizedAccessException => (
|
||||
StatusCodes.Status403Forbidden,
|
||||
ApiResponse<object>.Error(ErrorCodes.Forbidden, "无权访问该资源")),
|
||||
SharedValidationException validationException => (
|
||||
StatusCodes.Status422UnprocessableEntity,
|
||||
ApiResponse<object>.Error(ErrorCodes.ValidationFailed, "请求参数验证失败", validationException.Errors)),
|
||||
FluentValidationException fluentValidationException => (
|
||||
StatusCodes.Status422UnprocessableEntity,
|
||||
ApiResponse<object>.Error(
|
||||
ErrorCodes.ValidationFailed,
|
||||
"请求参数验证失败",
|
||||
NormalizeValidationErrors(fluentValidationException.Errors))),
|
||||
BusinessException businessException => (
|
||||
// 1. 仅当业务错误码在白名单且位于 400-499 时透传,否则回退 400
|
||||
AllowedHttpErrorCodes.Contains(businessException.ErrorCode) && businessException.ErrorCode is >= 400 and < 500
|
||||
@@ -93,4 +115,25 @@ public sealed class ExceptionHandlingMiddleware(RequestDelegate next, ILogger<Ex
|
||||
ApiResponse<object>.Error(ErrorCodes.InternalServerError, "服务器开小差啦,请稍后再试"))
|
||||
};
|
||||
}
|
||||
|
||||
private static IDictionary<string, string[]> NormalizeValidationErrors(IEnumerable<ValidationFailure> failures)
|
||||
{
|
||||
var result = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var failure in failures)
|
||||
{
|
||||
var key = string.IsNullOrWhiteSpace(failure.PropertyName) ? "request" : failure.PropertyName;
|
||||
if (!result.TryGetValue(key, out var list))
|
||||
{
|
||||
list = new List<string>();
|
||||
result[key] = list;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(failure.ErrorMessage))
|
||||
{
|
||||
list.Add(failure.ErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToDictionary(pair => pair.Key, pair => pair.Value.Distinct().ToArray(), StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user