You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.2 KiB

using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System;
using QualityControlPlatform.Helpers.Nlog;
using QualityControlPlatform.Helpers.Response;
namespace QualityControlPlatform.Controllers.Base
{
/// <summary>
/// 全局异常返回
/// </summary>
[Route("error")]
[ApiController]
public class ErrorController : ControllerBase
{
/// <summary>
/// 全局错误返回
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult Index()
{
//UseExceptionHandler中间件中拦截到异常时,会将异常信息保存在请求上下文中,所以我们可以从HttpContext中拿到ExceptionHandler的异常信息:
var feature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (feature == null) return ResponseHelper.ServerError();
var exception = feature.Error;
LogHelper.Log.Error(exception);
Console.WriteLine("【ERROR】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + exception.Message);
// todo:调用日志组件记录异常信息,或对异常做多路判断
return ResponseHelper.ServerError();
}
}
}