namespace OASystem.API.Middlewares
{
///
/// 全局异常捕获中间件
///
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next; // 用来处理上下文请求
private readonly ILogger _logger;
///
/// 初始化
///
///
///
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger logger)
{
_next = next;
_logger = logger;
}
///
/// 执行中间件
///
///
///
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext); //要么在中间件中处理,要么被传递到下一个中间件中去
}
catch (Exception ex)
{
await HandleExceptionAsync(httpContext, ex); // 捕获异常了 在HandleExceptionAsync中处理
}
}
///
/// 异步处理异常
///
///
///
///
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);
}
}
}