当前位置:网站首页>NetCore——自定义全局异常处理
NetCore——自定义全局异常处理
2022-08-06 03:03:00 【有诗亦有远方】
自定义全局异常处理
一、UserFriendlyException自定义用户友好错误消息
public class UserFriendlyException : BusinessException
{
public UserFriendlyException(
string message,
System.Exception innerException = null)
//message传递给父类BusinessException
: base(
message,
innerException)
{
}
}
二、BusinessException
//继承了系统异常
public class BusinessException : System.Exception, IBusinessException
{
public LogLevel LogLevel {
get; set; }
public BusinessException(
//UserFriendlyException(message)传递给Business的message
string message = null,
System.Exception innerException = null)
//传递给系统异常的message(异常消息)
: base(message, innerException)
{
}
}
三、自定义异常消息格式AjaxResponse
public class AjaxResponse<TData>
{
/// <summary>
/// 异常消息
/// </summary>
public string Message {
get; set; }
public AjaxResponse()
{
}
public AjaxResponse(TData data)
{
}
public AjaxResponse(string msg, bool success = false)
{
Message = msg;
}
public static AjaxResponse<TData> Result(bool isSuccess, int code, TData data, string message)
{
return new AjaxResponse<TData>
{
Message = message
};
}
四、StudentExceptionFilter自定义异常类
public class StudentExceptionFilter:IExceptionFilter
{
/// <summary>
/// 重写OnExceptionAsync方法,定义自己的处理逻辑
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public void OnException(ExceptionContext context)
{
//当使用UserFriendly后,自定义的异常消息,传入到了系统异常中
var exception = context.Exception; //获取异常消息的
//自定义错误消息的格式
var errorInfo = new AjaxResponse<object>("业务处理失败");
//AjaxResponse中的message设置为,返回的异常消息的message
errorInfo.Message = exception.Message;
//设置返回结果为AjaxResponse格式的消息
context.Result = new ObjectResult(errorInfo);
//设置该异常已被处理,因为异常处理不是只有一处,设置处理,其他地方将不在处理
context.ExceptionHandled = true; //Handled!
//设置异常为空
context.Exception = null; //Handled!
}
}
五、自定义异常注入到StartUp中
services.AddControllersWithViews(
options => {
options.Filters.Add(new StudentExceptionFilter());
}
)
边栏推荐
- A complete solution for serial port data reception in QT
- xctf攻防世界 Web高手进阶区 easytornado
- Two implementations of vtk patch hole
- Find the Nth node of the linked list
- The first day of learning MySQL: MySQL overview (basic)
- DevEco Studio配置:自定义头部代码注释
- LeetCode Daily 2 Questions 01: Sliding Window - Longest Look Without Repeating Characters
- ALV细节再梳理2022.8.5
- 新搭建的国家广告系列,是否要排除掉旧的广告系列?
- C 学生管理系统 删除指定学生节点(一般情况)
猜你喜欢
随机推荐
5、nerf++(pytorch)
详解AUTOSAR:什么是AUTOSAR?(理论篇—1)
预处理(C语言深度了解)
vtk 补洞 两种实现
【无标题】
学习MySQL的第二天:SQL(基础篇)
Camera calibration > > coordinate system transformation @ inside and outside the cords
走!VR技术带你沉浸式看展
迟来的初次见面
如何删除掉一张表的重复数据?
Online Question Feedback Module Actual Combat (20): Realize the function of batch exporting files to zip archives
Software Engineering - University Gymnasium Management System Class Diagram
WPF 截图控件之移除控件(九)「仿微信」
BYD's July sales exploded, and the product structure is very reasonable
入坑机器学习:三,非监督学习
美国国立卫生研究院(NIH)江晓芳组诚聘生物信息学博士后
Freemodbus 移植过程记录
How to build a set of invoicing management system (purchasing, sales, inventory integrated management) with low code?
Nanostar raises more than $200 million for battery material production
KGAT推荐系统









