From c40f8d664de6a5c7c025089eb20b94f427f9e284 Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 23 Apr 2024 12:27:36 +0800 Subject: [PATCH] default exception edit --- Controllers/SimApiBaseController.cs | 8 +++---- Helpers/SimApiUtil.cs | 11 ++++----- Middlewares/SimApiExceptionMiddleware.cs | 29 +++++++++++++----------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Controllers/SimApiBaseController.cs b/Controllers/SimApiBaseController.cs index b9a35e2..00c7001 100644 --- a/Controllers/SimApiBaseController.cs +++ b/Controllers/SimApiBaseController.cs @@ -55,7 +55,7 @@ public class SimApiBaseController : Controller /// 检测条件 /// 错误代码 /// 错误描述 - protected static void ErrorWhen(bool condition, int code = 500, string message = "") + protected static void ErrorWhen(bool condition, int code = 400, string message = "") { if (condition) { @@ -70,7 +70,7 @@ public class SimApiBaseController : Controller /// 检测条件 /// 错误代码 /// 错误描述 - protected static void ErrorWhenTrue(bool condition, int code = 500, string message = "") + protected static void ErrorWhenTrue(bool condition, int code = 400, string message = "") { ErrorWhen(condition, code, message); } @@ -82,7 +82,7 @@ public class SimApiBaseController : Controller /// /// /// - protected static void ErrorWhenFalse(bool condition, int code = 500, string message = "") + protected static void ErrorWhenFalse(bool condition, int code = 400, string message = "") { ErrorWhen(!condition, code, message); } @@ -93,7 +93,7 @@ public class SimApiBaseController : Controller /// 检测条件 /// 错误代码 /// 错误描述 - protected static void ErrorWhenNull(object condition, int code = 404, string message = "") + protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在") { ErrorWhen(condition == null, code, message); } diff --git a/Helpers/SimApiUtil.cs b/Helpers/SimApiUtil.cs index c659380..e1bdbae 100644 --- a/Helpers/SimApiUtil.cs +++ b/Helpers/SimApiUtil.cs @@ -51,16 +51,15 @@ public static class SimApiUtil /// public static string Md5(string source, string mode = "x2") { - var sor = Encoding.UTF8.GetBytes(source); - var md5 = MD5.Create(); - var result = md5.ComputeHash(sor); - var strbul = new StringBuilder(40); + var sourceBytes = Encoding.UTF8.GetBytes(source); + var result = MD5.HashData(sourceBytes); + var stringBuilder = new StringBuilder(40); foreach (var t in result) { - strbul.Append(t.ToString(mode)); + stringBuilder.Append(t.ToString(mode)); } - return strbul.ToString(); + return stringBuilder.ToString(); } /// diff --git a/Middlewares/SimApiExceptionMiddleware.cs b/Middlewares/SimApiExceptionMiddleware.cs index 10b3cb6..30099f4 100644 --- a/Middlewares/SimApiExceptionMiddleware.cs +++ b/Middlewares/SimApiExceptionMiddleware.cs @@ -24,12 +24,17 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger /// /// - private void ErrorResponse(HttpContext context, SimApiBaseResponse response) + private static void ErrorResponse(HttpContext context, SimApiBaseResponse response) { - if (!context.Response.HasStarted) - { - context.Response.StatusCode = 200; - context.Response.Headers.Append("Content-Type", "application/json"); - context.Response.WriteAsync(response.ToString()); - } + if (context.Response.HasStarted) return; + context.Response.StatusCode = 200; + context.Response.Headers.Append("Content-Type", "application/json"); + context.Response.WriteAsync(response.ToString()); } } \ No newline at end of file