123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- namespace OASystem.API.Middlewares
- {
- /// <summary>
- /// 全局异常捕获中间件
- /// </summary>
- public class ExceptionHandlingMiddleware
- {
- private readonly RequestDelegate _next; // 用来处理上下文请求
- private readonly ILogger<ExceptionHandlingMiddleware> _logger;
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="next"></param>
- /// <param name="logger"></param>
- public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
- {
- _next = next;
- _logger = logger;
- }
- /// <summary>
- /// 执行中间件
- /// </summary>
- /// <param name="httpContext"></param>
- /// <returns></returns>
- public async Task InvokeAsync(HttpContext httpContext)
- {
- try
- {
- await _next(httpContext); //要么在中间件中处理,要么被传递到下一个中间件中去
- }
- catch (Exception ex)
- {
- await HandleExceptionAsync(httpContext, ex); // 捕获异常了 在HandleExceptionAsync中处理
- }
- }
- /// <summary>
- /// 异步处理异常
- /// </summary>
- /// <param name="context"></param>
- /// <param name="exception"></param>
- /// <returns></returns>
- private async Task HandleExceptionAsync(HttpContext context, Exception exception)
- {
- context.Response.ContentType = "application/json"; // 返回json 类型
- var response = context.Response;
- var errorResponse = new JsonView
- {
- Code = StatusCodes.Status500InternalServerError,
- Data = ""
- }; // 自定义的异常错误信息类型
- switch (exception)
- {
- case ApplicationException ex:
- if (ex.Message.Contains("Invalid token"))
- {
- response.StatusCode = StatusCodes.Status403Forbidden;
- errorResponse.Msg = ex.Message;
- break;
- }
- response.StatusCode = StatusCodes.Status400BadRequest;
- errorResponse.Msg = ex.Message;
- break;
- case KeyNotFoundException ex:
- response.StatusCode = StatusCodes.Status404NotFound;
- errorResponse.Msg = ex.Message;
- break;
- default:
- response.StatusCode = StatusCodes.Status500InternalServerError;
- errorResponse.Msg = exception.Message;
- break;
- }
- _logger.LogError(exception.Message);
- var result = JsonConvert.SerializeObject(errorResponse);
- await context.Response.WriteAsync(result);
- }
- }
- }
|