.net core异常中间件的使用

if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }

这样写入中间件哈,那么在env环境下就会去执行UseDeveloperExceptionPage。

public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder app) { if (app == null) { throw new ArgumentNullException(nameof(app)); } return app.UseMiddleware<DeveloperExceptionPageMiddleware>(); }

那么我们应该去看DeveloperExceptionPageMiddleware中间件哈。

那么这里介绍它是如何能够捕获其他中间件的异常的哈。

里面的invoke:

其实它的操作是很简单的,直接在外面套了try catch。

里面的异常处理怎么处理的可以直接去看DeveloperExceptionPageMiddleware 中间件,里面的操作也比较简单处理。

测试:

[HttpGet] public int GetService([FromServices]ISelfService selfService) { throw new System.Exception("错误"); return 1; }

结果:

.net core异常中间件的使用

因为上面说了,这个是dev环境下,那么生产环境不能直接给用户看到错误信息。

正式环境:

app.UseExceptionHandler("/error");

将错误转移到/error 处理。具体UseExceptionHandler细节篇里面介绍,有许多可以借鉴的地方。

[ApiController] [Route("[controller]")] public class ErrorController : Controller { public ILogger<ErrorController> _logger; public ErrorController(ILogger<ErrorController> logger) { this._logger = logger; } public IActionResult Index() { var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>(); var ex = exceptionHandlerPathFeature?.Error; var knownException = ex as IKnownException; if (knownException == null) { _logger.LogError(ex, ex.Message); knownException = KnownException.Unknow; } else { knownException = KnownException.FromKnowException(knowException); } return View(knownException); } }

视图:

<html> <head> </head> <body> <div> 错误码: @Model.ErrorCode </div> <div> 错误信息: @Model.Message </div> </body> </html>

IKnownException:

public interface IKnownException { public string Message { get; } public int ErrorCode { get; } public object[] ErrorData { get; } }

KnownException:

public class KnownException : IKnownException { public string Message { get; private set; } public int ErrorCode { get; private set; } public object[] ErrorData { get; private set; } public readonly static IKnownException Unknow = new KnownException { Message = "未知错误", ErrorCode = 99 }; public static IKnownException FromKnowException(IKnownException Exception) { return new KnownException{Message = Exception.Message, ErrorCode = Exception.ErrorCode, ErrorData = Exception.ErrorData}; } }

测试1:

[HttpGet] public int GetService([FromServices]ISelfService selfService) { throw new System.Exception("错误"); return 1; }

这种属于未知异常,结果:

.net core异常中间件的使用

现在弄一个支付异常:

public class PayErrorException : Exception, IKnownException { public PayErrorException(string message, int errorCode, params object[] errorData): base(message) { this.ErrorCode = errorCode; this.ErrorData = errorData; } public int ErrorCode { get;private set; } public object[] ErrorData { get;private set; } }

测试2:

[HttpGet] public int GetService([FromServices]ISelfService selfService) { throw new PayErrorException("支付错误",405,null); return 1; }

.net core异常中间件的使用

将异常处理放入到中间件分支中。

app.UseExceptionHandler(errApp => { errApp.Run(async context => { var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>(); IKnownException knownException = exceptionHandlerPathFeature.Error as IKnownException; if (knownException == null) { var logger = context.RequestServices.GetService<ILogger<MyExceptionFilterAttribute>>(); logger.LogError(exceptionHandlerPathFeature.Error, exceptionHandlerPathFeature.Error.Message); knownException = KnownException.Unknown; context.Response.StatusCode = StatusCodes.Status500InternalServerError; } else { knownException = KnownException.FromKnownException(knownException); context.Response.StatusCode = StatusCodes.Status200OK; } var jsonOptions = context.RequestServices.GetService<IOptions<JsonOptions>>(); context.Response.ContentType = "application/json; charset=utf-8"; await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(knownException, jsonOptions.Value.JsonSerializerOptions)); }); });

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfdzw.html