Util应用框架基础(五) - 异常处理
本节介绍Util应用框架如何处理系统错误.
概述
系统在运行过程中可能发生错误.
系统错误可以简单分为两类:
系统异常
系统本身出现的错误.
业务异常
不满足业务规则出现的错误.
如何处理系统异常
如果发生系统异常,大多数情况下,你除了记录异常日志外,可能无法处理它们.
一个例外是并发异常.
当发生并发异常,可以通过重试再次提交,有可能成功处理.
另外一个问题,是否应该将系统异常消息返回给客户端?
系统异常消息与技术相关,客户无法理解它们.
而且系统异常消息可能包含敏感信息,返回给客户端可能更易受到攻击.
如何处理业务异常
业务异常表明没有满足某些业务规则,通常也无法自动处理.
如果能够自动处理的业务异常,应定义专用异常类型.
对于业务异常,除了记录异常日志外,还应把业务异常消息返回给客户端,以指示用户调整操作.
基础用法
.Net 使用异常 Exception 及派生异常来处理系统异常,但没有明确规定处理业务异常的类型.
Warning 业务异常
Util应用框架定义了 Util.Exceptions.Warning 异常类型,Warning 从 Exception 派生,代表业务异常.
当你抛出 Exception 或派生异常类型时,异常消息仅在开发阶段返回给客户端.
一旦发布到生产环境,系统异常消息将被屏蔽,客户端收到消息: 系统忙,请稍后再试 .
throw new Exception( "未将对象引用设置到对象的实例" );
对于业务规则导致的错误,你需要抛出 Warning 异常.
Warning 抛出的异常消息将返回到客户端,提示用户进行修改.
throw new Warning( "必须填写姓名" );
GetMessage 工具方法
Warning 除了代表业务异常外,还提供了一个静态工具方法 GetMessage.
异常可能被其它异常包裹,要获得异常真正的消息,需要使用递归.
Warning.GetMessage 工具方法传入异常实例,递归获取异常消息,
var message = Warning.GetMessage( exception );
ConcurrencyException 并发异常
Util应用框架定义了并发异常 Util.Exceptions.ConcurrencyException.
不同的 .Net 组件抛出的并发异常类型可能不同, Util使用 ConcurrencyException 进行统一包装.
可以通过重试的方式来解决并发异常.
下面是Util应用框架Dapr集成事件增加计数时并发处理的代码片断.
public virtual async Task IncrementAsync( CancellationToken cancellationToken = default ) {
try {
await Store.IncrementAsync( cancellationToken );
}
catch ( ConcurrencyException ) {
Log.LogDebug( "更新集成事件计数出现并发异常,即将重试" );
await IncrementAsync( cancellationToken );
}
catch ( Exception exception ) {
Log.LogError( exception, "更新集成事件计数失败" );
}
}
全局错误日志记录
Util应用框架使用 ErrorLogFilterAttribute 过滤器来记录全局错误日志.
已在 Web Api控制器基类 WebApiControllerBase 设置 [ErrorLogFilter] 过滤器.
全局异常处理
Util应用框架使用 ExceptionHandlerAttribute 过滤器来处理全局异常.
已在 Web Api控制器基类 WebApiControllerBase 设置 [ExceptionHandler] 过滤器.
[ExceptionHandler] 过滤器对异常消息进行处理,只有 Warning 异常消息才会返回给客户端.
源码解析
Warning 业务异常
Warning 代表业务异常,它的异常消息会返回给客户端.
GetMessage 方法使用递归获取内部异常消息.
/// <summary>
/// 应用程序异常
/// </summary>
public class Warning : Exception {
/// <summary>
/// 初始化应用程序异常
/// </summary>
/// <param name="exception">异常</param>
public Warning( Exception exception )
: this( null, exception ) {
}
/// <summary>
/// 初始化应用程序异常
/// </summary>
/// <param name="message">错误消息</param>
/// <param name="exception">异常</param>
/// <param name="code">错误码</param>
/// <param name="httpStatusCode">Http状态码</param>
public Warning( string message, Exception exception = null, string code = null, int? httpStatusCode = null )
: base( message ?? "", exception ) {
Code = code;
HttpStatusCode = httpStatusCode;
IsLocalization = true;
}
/// <summary>
/// 错误码
/// </summary>
public string Code { get; set; }
/// <summary>
/// Http状态码
/// </summary>
public int? HttpStatusCode { get; set; }
/// <summary>
/// 是否本地化异常消息
/// </summary>
public bool IsLocalization { get; set; }
/// <summary>
/// 获取错误消息
/// </summary>
/// <param name="isProduction">是否生产环境</param>
public virtual string GetMessage( bool isProduction = false ) {
return GetMessage( this );
}
/// <summary>
/// 获取错误消息
/// </summary>
public static string GetMessage( Exception ex ) {
var result = new StringBuilder();
var list = GetExceptions( ex );
foreach( var exception in list )
AppendMessage( result, exception );
return result.ToString().Trim( Environment.NewLine.ToCharArray() );
}
/// <summary>
/// 添加异常消息
/// </summary>
private static void AppendMessage( StringBuilder result, Exception exception ) {
if( exception == null )
return;
result.AppendLine( exception.Message );
}
/// <summary>
/// 获取异常列表
/// </summary>
public IList<Exception> GetExceptions() {
return GetExceptions( this );
}
/// <summary>
/// 获取异常列表
/// </summary>
/// <param name="ex">异常</param>
public static IList<Exception> GetExceptions( Exception ex ) {
var result = new List<Exception>();
AddException( result, ex );
return result;
}
/// <summary>
/// 添加内部异常
/// </summary>
private static void AddException( List<Exception> result, Exception exception ) {
if( exception == null )
return;
result.Add( exception );
AddException( result, exception.InnerException );
}
}
ConcurrencyException 并发异常
ConcurrencyException 表示并发异常,统一包装其它组件产生的并发异常,并处理异常消息.
/// <summary>
/// 并发异常
/// </summary>
public class ConcurrencyException : Warning {
/// <summary>
/// 消息
/// </summary>
private readonly string _message;
/// <summary>
/// 初始化并发异常
/// </summary>
public ConcurrencyException()
: this( "" ) {
}
/// <summary>
/// 初始化并发异常
/// </summary>
/// <param name="exception">异常</param>
public ConcurrencyException( Exception exception )
: this( "", exception ) {
}
/// <summary>
/// 初始化并发异常
/// </summary>
/// <param name="message">错误消息</param>
/// <param name="exception">异常</param>
/// <param name="code">错误码</param>
/// <param name="httpStatusCode">Http状态码</param>
public ConcurrencyException( string message, Exception exception = null, string code = null, int? httpStatusCode = null )
: base( message, exception, code, httpStatusCode ) {
_message = message;
}
/// <inheritdoc />
public override string Message => $"{R.ConcurrencyExceptionMessage}.{_message}";
/// <inheritdoc />
public override string GetMessage( bool isProduction = false ) {
if( isProduction )
return R.ConcurrencyExceptionMessage;
return GetMessage(this);
}
}
ErrorLogFilterAttribute 错误日志过滤器
[ErrorLogFilter] 错误日志过滤器记录全局异常日志.
/// <summary>
/// 错误日志过滤器
/// </summary>
public class ErrorLogFilterAttribute : ExceptionFilterAttribute {
/// <summary>
/// 异常处理
/// </summary>
public override void OnException( ExceptionContext context ) {
if( context == null )
return;
var log = context.HttpContext.RequestServices.GetService<ILogger<ErrorLogFilterAttribute>>();
var exception = context.Exception.GetRawException();
if( exception is Warning warning ) {
log.LogWarning( warning, exception.Message );
return;
}
log.LogError( exception, exception.Message );
}
}
ExceptionHandlerAttribute 异常处理过滤器
[ExceptionHandler] 过滤器处理全局异常.
Exception 的扩展方法 GetPrompt 获取客户端友好的异常消息.
对于生产环境, Exception 异常消息将被替换为 系统忙,请稍后再试.
[ExceptionHandler] 过滤器还对异常消息的本地化进行了处理.
/// <summary>
/// 异常处理过滤器
/// </summary>
public class ExceptionHandlerAttribute : ExceptionFilterAttribute {
/// <summary>
/// 异常处理
/// </summary>
public override void OnException( ExceptionContext context ) {
context.ExceptionHandled = true;
var message = context.Exception.GetPrompt( Web.Environment.IsProduction() );
message = GetLocalizedMessages( context, message );
var errorCode = context.Exception.GetErrorCode() ?? StateCode.Fail;
var httpStatusCode = context.Exception.GetHttpStatusCode() ?? 200;
context.Result = GetResult( context, errorCode, message, httpStatusCode );
}
/// <summary>
/// 获取本地化异常消息
/// </summary>
protected virtual string GetLocalizedMessages( ExceptionContext context, string message ) {
var exception = context.Exception.GetRawException();
if ( exception is Warning { IsLocalization: false } )
return message;
var stringLocalizerFactory = context.HttpContext.RequestServices.GetService<IStringLocalizerFactory>();
if ( stringLocalizerFactory == null )
return message;
var stringLocalizer = stringLocalizerFactory.Create( "Warning",null );
var localizedString = stringLocalizer[message];
if ( localizedString.ResourceNotFound == false )
return localizedString.Value;
stringLocalizer = context.HttpContext.RequestServices.GetService<IStringLocalizer>();
if ( stringLocalizer == null )
return message;
return stringLocalizer[message];
}
/// <summary>
/// 获取结果
/// </summary>
protected virtual IActionResult GetResult( ExceptionContext context, string code, string message, int? httpStatusCode ) {
var options = GetJsonSerializerOptions( context );
var resultFactory = context.HttpContext.RequestServices.GetService<IResultFactory>();
if ( resultFactory == null )
return new Result( code, message, null, httpStatusCode, options );
return resultFactory.CreateResult( code, message, null, httpStatusCode, options );
}
/// <summary>
/// 获取Json序列化配置
/// </summary>
private JsonSerializerOptions GetJsonSerializerOptions( ExceptionContext context ) {
var factory = context.HttpContext.RequestServices.GetService<IJsonSerializerOptionsFactory>();
if( factory != null )
return factory.CreateOptions();
return new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create( UnicodeRanges.All ),
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = {
new DateTimeJsonConverter(),
new NullableDateTimeJsonConverter()
}
};
}
}
/// <summary>
/// 异常扩展
/// </summary>
public static class ExceptionExtensions {
/// <summary>
/// 获取异常提示
/// </summary>
/// <param name="exception">异常</param>
/// <param name="isProduction">是否生产环境</param>
public static string GetPrompt( this Exception exception, bool isProduction = false ) {
if( exception == null )
return null;
exception = exception.GetRawException();
if( exception == null )
return null;
if( exception is Warning warning )
return warning.GetMessage( isProduction );
return isProduction ? R.SystemError : exception.Message;
}
/// <summary>
/// 获取Http状态码
/// </summary>
/// <param name="exception">异常</param>
public static int? GetHttpStatusCode( this Exception exception ) {
if ( exception == null )
return null;
exception = exception.GetRawException();
if ( exception == null )
return null;
if ( exception is Warning warning )
return warning.HttpStatusCode;
return null;
}
/// <summary>
/// 获取错误码
/// </summary>
/// <param name="exception">异常</param>
public static string GetErrorCode( this Exception exception ) {
if ( exception == null )
return null;
exception = exception.GetRawException();
if ( exception == null )
return null;
if ( exception is Warning warning )
return warning.Code;
return null;
}
}
Util应用框架基础(五) - 异常处理的更多相关文章
- 爬虫基础(五)-----scrapy框架简介
---------------------------------------------------摆脱穷人思维 <五> :拓展自己的视野,适当做一些眼前''无用''的事情,防止进入只关 ...
- Hadoop框架基础(五)
** Hadoop框架基础(五) 已经部署了Hadoop的完全分布式集群,我们知道NameNode节点的正常运行对于整个HDFS系统来说非常重要,如果NameNode宕掉了,那么整个HDFS就要整段垮 ...
- HBase框架基础(五)
* HBase框架基础(五) 本节主要介绍HBase中关于分区的一些知识. * HBase的RowKey设计 我们为什么要讨论rowKey的设计?或者说为什么很多工作岗位要求有rowKey的优化设计经 ...
- Hibernatel框架基础使用
Hibernatel框架基础使用 1.简介 1.1.Hibernate框架由来 Struts:基于MVC模式的应用层框架技术 Hibernate:基于持久层的框架(数据访问层使用)! Spring:创 ...
- .NET面试题系列[1] - .NET框架基础知识(1)
很明显,CLS是CTS的一个子集,而且是最小的子集. - 张子阳 .NET框架基础知识(1) 参考资料: http://www.tracefact.net/CLR-and-Framework/DotN ...
- 框架基础之Hibernate简介
框架基础之Hibernate简介 1.什么是Hibernate? Hibernate是一个开发源代码的对象关系映射框架,它对JDBC进行非常轻量级的对象封装,使得程序员可以随心所欲地使用对象编程思维来 ...
- 游戏UI框架设计(五): 配置管理与应用
游戏UI框架设计(五) --配置管理与应用 在开发企业级游戏/VR/AR产品时候,我们总是希望可以总结出一些通用的技术体系,框架结构等,为简化我们的开发起到"四两拨千金"的作用.所 ...
- c#基础之异常处理
在开发过程中,经常遇到各种各样的小问题,很多是由于基础不牢固,没有经常总结导致的.遇到重复的问题可能可根据以往经验处理,但是对问题本身引发的原因进行深入的了解.工作很多年,但是c#基础像一层冰一样,可 ...
- c#基础之异常处理及自定义异常 从SQLServer转储数据到MySQL
c#基础之异常处理及自定义异常 一.什么是c#中的异常? 异常是程序运行中发生的错误,异常处理是程序的一部分.c#中的异常类主要是直接或者间接的派生于 System.Exception类 ,也就是说S ...
- Hibernate框架基础
Hibernate框架基础 Hibernate框架 ORM概念 O, Object 对象 R, Realtion 关系 (关系型数据库: MySQL, Oracle…) M,Mapping 映射 OR ...
随机推荐
- eclipse module-info.java文件
module 本模块的名称{ exports 对外暴露的包路径; requires 需要依赖的其他模块名称; } module-info.java不是类,不是接口,是一些模块描述信息.module也不 ...
- pycharm链接mysql报错: Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.
检查驱动 我本机安装的mysql版本是5.6的,那么IDEA要连接mysql也应该匹配下驱动版本.把Driver改成MySQL for 5.1就可以了. 参考链接:https://blog.csdn. ...
- win10安装mysql5.7.35教程
前提条件:我下载的是压缩包版本5.7.35,下载地址是 https://downloads.mysql.com/archives/community/ 下载完后解压,并在如下图目录里加入data文件夹 ...
- 两种方式,轻松实现ChatGPT联网
两种方式效果: 方式一:浏览器搜索内嵌插件 方式二:官方聊天页内嵌插件 首先,要有一个谷歌浏览器,然后再安装一个叫ChatGPT for Google,直接在谷歌里搜一下就能找,也可以Chrome应用 ...
- (2023.7.15)软件加密与解密-番外1-PWN2REVERSE[XDbg]
/提示:如果你看到了这行文字,那说明您的预览器不支持内嵌 CSS 代码,请使用 VSCode 阅读本 Markdown 文件/ 每天一个技术点 (2023.7.15)软件加密与解密-番外1-PWN2R ...
- 升级java11后,maven命令打包报错
一.问题 升级java11后,maven命令打包报错: mvn clean package -Dmaven.test.skip=true [ERROR] Failed to execute goal ...
- 避坑|在读取excel.xlsx文件中的内容时发现明明只有3行,但跑起来却认为有13行,导致有10行None,UI自动化测试代码空跑了10次;|UI自动化测试|数据驱动
在读取excel.xlsx文件中的内容时发现明明只有3行,但跑起来却认为有13行,导致有10行None,UI自动化测试代码空跑了10次: 原因:excel.xlsx内容清除时用delete快捷键导致, ...
- Htttpclien循环自动生成图片,同时发送参数和文件,模拟http的post请求
package org.jeecg.modules.bussiness.PostTests; import com.sun.tools.internal.xjc.reader.xmlschema.bi ...
- 2018-D
2018-D 新建数据库 test0317,目录为考试目录,并在完成建表后备份 1.建表: use [test0317]; create table [STD_INFO]( [std_id] int ...
- Python爬虫IP代理池的建立和使用
写在前面建立Python爬虫IP代理池可以提高爬虫的稳定性和效率,可以有效避免IP被封锁或限制访问等问题. 下面是建立Python爬虫IP代理池的详细步骤和代码实现: 1. 获取代理IP我们可以从一些 ...