YD_XinWei.Api/YD_XinWei.Commons/Middlewares/SystemExceptionHandlerderMiddleware.cs

80 lines
2.5 KiB
C#
Raw Normal View History

2025-01-13 21:06:59 +08:00
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace YD_XinWei.Commons.Middlewares
{
/// <summary>
/// 自定义异常中间件,对于系统异常的处理, 超时异常,异常。
/// 拦截Response输出流进行统一异常处理
/// </summary>
public class SystemExceptionHandlerderMiddleware
{
private readonly RequestDelegate _next;
public SystemExceptionHandlerderMiddleware(RequestDelegate next)
{
this._next = next;
}
/// <summary>
/// 执行异常处理(执行action无法拦截的异常)
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
public async Task Invoke(HttpContext httpContext)
{
try
{
// 1、执行下一个中间件
await _next(httpContext);
/* var statusCode = httpContext.Response.StatusCode;
var msg = "";
if (statusCode == 401)
{
msg = "未授权";
}
else if (statusCode == 404)
{
msg = "未找到服务";
}
else if (statusCode == 502)
{
msg = "请求错误";
}
else if (statusCode != 200)
{
msg = httpContext.Response.Body.ToString();
}
if (!string.IsNullOrWhiteSpace(msg))
{
await HandleExceptionAsync(httpContext, statusCode, msg);
}*/
}
catch (System.Exception ex)
{
await HandleExceptionAsync(httpContext, httpContext.Response.StatusCode, ex.Message);
}
}
private async static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
{
context.Response.ContentType = "application/json;charset=utf-8";
// 1、异常结果转换成json格式输出
dynamic warpResult = new ExpandoObject();
2025-01-14 14:51:07 +08:00
warpResult.ErrorCode = "-1";
warpResult.ErrorMsg = msg;
2025-01-13 21:06:59 +08:00
// 2、异常json格式输出
var stream = context.Response.Body;
await JsonSerializer.SerializeAsync(stream, warpResult);
}
}
}