增加一些 报错信息统一拦截
This commit is contained in:
1 parent
c9ac7412ab
commit
4a5fada435
3 files changed
+26
-23
No files matched your search
@@ -2,6 +2,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
@@ -12,10 +13,12 @@ namespace MAX.Framework.Commons
|
||||
public class GlobalExceptionFilter : IExceptionFilter
|
||||
{
|
||||
private readonly ILogger<GlobalExceptionFilter> _logger;
|
||||
private readonly IHostEnvironment _environment;
|
||||
|
||||
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
|
||||
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger, IHostEnvironment environment)
|
||||
{
|
||||
_logger = logger;
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
public void OnException(ExceptionContext context)
|
||||
@@ -24,10 +27,13 @@ namespace MAX.Framework.Commons
|
||||
|
||||
var result = context.Exception switch
|
||||
{
|
||||
AppException be => ApiResult.Fail(be.Message, be.Code),
|
||||
ValidationException ve => ApiResult.Fail(ve.Message, ResultCode.ValidationError),
|
||||
UnauthorizedAccessException => ApiResult.Fail("未授权访问", ResultCode.Unauthorized),
|
||||
_ => ApiResult.Fail("服务器内部错误", ResultCode.ServerError)
|
||||
AppException be => ApiResult.Fail(be.Message, be.Code, traceId),
|
||||
ArgumentException arge => ApiResult.Fail(arge.Message, ResultCode.ValidationError, traceId),
|
||||
ValidationException ve => ApiResult.Fail(ve.Message, ResultCode.ValidationError, traceId),
|
||||
UnauthorizedAccessException => ApiResult.Fail("未授权访问", ResultCode.Unauthorized, traceId),
|
||||
_ => ApiResult.Fail(
|
||||
_environment.IsDevelopment() ? context.Exception.Message : "服务器内部错误",
|
||||
ResultCode.ServerError, traceId)
|
||||
};
|
||||
|
||||
_logger.LogError(context.Exception, "请求异常 TraceId: {TraceId}", traceId);
|
||||
|
||||
Reference in new issue
Block a user