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 ...
随机推荐
- Linux 日志收集器:syslog,syslog-ng,rsyslog
一 参考:https://www.cnblogs.com/zhaoyong631/p/14441090.html 基本上,它们都是相同,它们都允许在中央存储库中记录来自不同类型系统的数据. 但是它们是 ...
- 2022-1-20 Wpf绑定属性
使用UpdateSourceTrigger绑定属性 后台绑定 通过后台代码绑定 UpdateSourceTrigger
- 2021-7-7 VUE笔记2
if实例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <scri ...
- html5 2.0学习
列表定义:是一种特别的对象集合.集合:集中在一起合二为一(聚集). 聚集:多个列(信息资源)排在一起.信息资源:一堆数据,可能是字符,可能是图片. 列表分类:有序列表 无序列表 (自)定义列表 有 ...
- 表格JS实现在线Excel的附件上传与下载
摘要:本文由葡萄城技术团队于博客园原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 在本地使用Excel时,经常会有需要在Excel中添加一 ...
- CF-1860C Game on Permutation题解
题意:在一条数轴上,Alice可以跳到在你所在点前面且值比当前所在点小的点.每回合可以向任意符合要求的点跳一次.当轮到Alice的回合同时不存在符合要求的点,Alice就赢了.Alice可以选择一个点 ...
- [ABC143E] Travel by Car
2023-02-20 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4.5 题目来源 AtCoder 题目算法 最短路 解题思路 我们枚举每一对点 \((u_i,v_i)\) 间的距 ...
- 国内镜像安装Python解释器及扩展包
一.下载Python解释器 1.下载地址 官网(下载速度很慢):Welcome to Python.org 淘宝镜像(推荐):CNPM Binaries Mirror (npmmirror.com) ...
- Linux下Python环境安装
Linux通常都附带Python环境,但是Linux附带的大多数Python都是2.7.5版本.如果我们想使用Python3或者Anaconda3,最好安装一个新的Python3环境,但不要尝试删除P ...
- Linux-Stream内存带宽及MLC内存延迟性能测试方法
1.Stream内存带宽测试 Stream是业界主流的内存带宽测试程序,测试行为相对简单可控.该程序对CPU的计算能力要求很小,对CPU内存带宽压力很大.随着处理器核心数量的增大,而内存带宽并没有 ...