Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.
Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
fa1b286463
241 files changed
+31188
No files matched your search
@@ -0,0 +1,77 @@
|
||||
using System.Net;
|
||||
using WmsLite.Api.Common;
|
||||
|
||||
namespace WmsLite.Api.Infrastructure;
|
||||
|
||||
public class GlobalExceptionMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<GlobalExceptionMiddleware> _logger;
|
||||
|
||||
public GlobalExceptionMiddleware(RequestDelegate next, ILogger<GlobalExceptionMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
await WriteResultAsync(context, HttpStatusCode.BadRequest, BusinessResult.Fail(ex.Message, ex.Code));
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
await WriteResultAsync(context, HttpStatusCode.Unauthorized, BusinessResult.Fail(ex.Message, "401"));
|
||||
}
|
||||
catch (Exception ex) when (IsUniqueConstraintViolation(ex))
|
||||
{
|
||||
await WriteResultAsync(
|
||||
context,
|
||||
HttpStatusCode.BadRequest,
|
||||
BusinessResult.BadRequest("数据已存在或违反唯一约束,请更换编码后重试。"));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Invalid operation.");
|
||||
await WriteResultAsync(context, HttpStatusCode.InternalServerError, BusinessResult.Fail("服务器内部错误。"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Unhandled API exception.");
|
||||
await WriteResultAsync(context, HttpStatusCode.InternalServerError, BusinessResult.Fail("服务器内部错误。"));
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsUniqueConstraintViolation(Exception ex)
|
||||
{
|
||||
for (var cur = ex; cur is not null; cur = cur.InnerException)
|
||||
{
|
||||
var msg = cur.Message;
|
||||
if (msg.Contains("23505", StringComparison.Ordinal)
|
||||
|| msg.Contains("duplicate key", StringComparison.OrdinalIgnoreCase)
|
||||
|| msg.Contains("unique constraint", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static async Task WriteResultAsync(HttpContext context, HttpStatusCode statusCode, BusinessResult result)
|
||||
{
|
||||
if (context.Response.HasStarted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.Response.StatusCode = (int)statusCode;
|
||||
context.Response.ContentType = "application/json; charset=utf-8";
|
||||
await context.Response.WriteAsJsonAsync(result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user