From da07c2722111f78b51aedc9c51b70d5f1de032de Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Sat, 8 Oct 2022 14:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BF=AE=E6=94=B9api=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E7=AD=9B=E9=80=89=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpResponseExceptionFilter.cs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Falcon.SugarApi/ApiDefinistions/HttpResponseExceptionFilter.cs b/Falcon.SugarApi/ApiDefinistions/HttpResponseExceptionFilter.cs index 5dbf0d0..63c6656 100644 --- a/Falcon.SugarApi/ApiDefinistions/HttpResponseExceptionFilter.cs +++ b/Falcon.SugarApi/ApiDefinistions/HttpResponseExceptionFilter.cs @@ -6,6 +6,9 @@ using System.Text; namespace Falcon.SugarApi.ApiDefinistions { + /// + /// 异常处理筛选器 + /// public class HttpResponseExceptionFilter : IActionFilter, IOrderedFilter { /// @@ -13,32 +16,36 @@ namespace Falcon.SugarApi.ApiDefinistions /// public ILogger Logger { get; set; } - public HttpResponseExceptionFilter(ILogger logger) - { + /// + /// api异常状态码 + /// + public int StatusCode { get; set; } = 400; + + /// + public HttpResponseExceptionFilter(ILogger logger) { this.Logger = logger; } + /// public int Order { get; } = int.MaxValue - 10; + /// public void OnActionExecuting(ActionExecutingContext context) { } - public void OnActionExecuted(ActionExecutedContext context) - { - if (context.Exception == null || context.ExceptionHandled) - { + /// + public void OnActionExecuted(ActionExecutedContext context) { + if (context.Exception == null || context.ExceptionHandled) { return; } var exception = context.Exception; var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - var model = new ExceptionModel - { + var model = new ExceptionModel { Id = Guid.NewGuid().ToString(), CreateDateTime = now, Message = exception.Message, }; - if (exception is ApiException) - { - context.Result = new ObjectResult(model) { StatusCode = 400, }; + if (exception is ApiException) { + context.Result = new ObjectResult(model) { StatusCode = this.StatusCode, }; context.ExceptionHandled = true; } var logmsg = new StringBuilder();