|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Runtime.Serialization; |
| 1 | +using System.Runtime.Serialization; |
4 | 2 | using Bing.Exceptions; |
5 | 3 |
|
6 | | -namespace Bing |
| 4 | +namespace Bing; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Bing异常 |
| 8 | +/// </summary> |
| 9 | +[Serializable] |
| 10 | +public abstract class BingException : Exception |
7 | 11 | { |
8 | 12 | /// <summary> |
9 | | - /// Bing异常 |
| 13 | + /// 空标识 |
| 14 | + /// </summary> |
| 15 | + protected const string EmptyFlag = "__EMPTY_FLG"; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// 默认错误消息 |
| 19 | + /// </summary> |
| 20 | + protected const string DefaultErrorMessage = "_DEFAULT_ERROR"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// 默认错误编码 |
| 24 | + /// </summary> |
| 25 | + protected const long DefaultErrorCode = 1001; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// 默认扩展错误编码 |
| 29 | + /// </summary> |
| 30 | + protected const long DefaultExtendErrorCode = 1002; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 34 | + /// </summary> |
| 35 | + protected BingException() : this(DefaultErrorCode, DefaultErrorMessage, EmptyFlag) { } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 39 | + /// </summary> |
| 40 | + /// <param name="errorMessage">错误消息</param> |
| 41 | + /// <param name="innerException">内部异常</param> |
| 42 | + protected BingException(string errorMessage, Exception innerException = null) : this(errorMessage, EmptyFlag, innerException) { } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 46 | + /// </summary> |
| 47 | + /// <param name="errorMessage">错误消息</param> |
| 48 | + /// <param name="flag">错误标识</param> |
| 49 | + /// <param name="innerException">内部异常</param> |
| 50 | + protected BingException(string errorMessage, string flag, Exception innerException = null) : this(DefaultExtendErrorCode, errorMessage, flag, innerException) { } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 54 | + /// </summary> |
| 55 | + /// <param name="errorCode">错误码</param> |
| 56 | + /// <param name="errorMessage">错误消息</param> |
| 57 | + /// <param name="innerException">内部异常</param> |
| 58 | + protected BingException(long errorCode, string errorMessage, Exception innerException = null) : this(errorCode, errorMessage, EmptyFlag, innerException) { } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
10 | 62 | /// </summary> |
11 | | - [Serializable] |
12 | | - public abstract class BingException : Exception |
| 63 | + /// <param name="info">序列化信息</param> |
| 64 | + /// <param name="context">流上下文</param> |
| 65 | + protected BingException(SerializationInfo info, StreamingContext context) : base(info, context) |
13 | 66 | { |
14 | | - /// <summary> |
15 | | - /// 空标识 |
16 | | - /// </summary> |
17 | | - protected const string EmptyFlag = "__EMPTY_FLG"; |
18 | | - |
19 | | - /// <summary> |
20 | | - /// 默认错误消息 |
21 | | - /// </summary> |
22 | | - protected const string DefaultErrorMessage = "_DEFAULT_ERROR"; |
23 | | - |
24 | | - /// <summary> |
25 | | - /// 默认错误编码 |
26 | | - /// </summary> |
27 | | - protected const long DefaultErrorCode = 1001; |
28 | | - |
29 | | - /// <summary> |
30 | | - /// 默认扩展错误编码 |
31 | | - /// </summary> |
32 | | - protected const long DefaultExtendErrorCode = 1002; |
33 | | - |
34 | | - /// <summary> |
35 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
36 | | - /// </summary> |
37 | | - protected BingException() : this(DefaultErrorCode, DefaultErrorMessage, EmptyFlag) { } |
38 | | - |
39 | | - /// <summary> |
40 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
41 | | - /// </summary> |
42 | | - /// <param name="errorMessage">错误消息</param> |
43 | | - /// <param name="innerException">内部异常</param> |
44 | | - protected BingException(string errorMessage, Exception innerException = null) : this(errorMessage, EmptyFlag, innerException) { } |
45 | | - |
46 | | - /// <summary> |
47 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
48 | | - /// </summary> |
49 | | - /// <param name="errorMessage">错误消息</param> |
50 | | - /// <param name="flag">错误标识</param> |
51 | | - /// <param name="innerException">内部异常</param> |
52 | | - protected BingException(string errorMessage, string flag, Exception innerException = null) : this(DefaultExtendErrorCode, errorMessage, flag, innerException) { } |
53 | | - |
54 | | - /// <summary> |
55 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
56 | | - /// </summary> |
57 | | - /// <param name="errorCode">错误码</param> |
58 | | - /// <param name="errorMessage">错误消息</param> |
59 | | - /// <param name="innerException">内部异常</param> |
60 | | - protected BingException(long errorCode, string errorMessage, Exception innerException = null) : this(errorCode, errorMessage, EmptyFlag, innerException) { } |
61 | | - |
62 | | - /// <summary> |
63 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
64 | | - /// </summary> |
65 | | - /// <param name="info">序列化信息</param> |
66 | | - /// <param name="context">流上下文</param> |
67 | | - protected BingException(SerializationInfo info, StreamingContext context) : base(info, context) |
68 | | - { |
69 | | - ExtraData = new Dictionary<string, object>(); |
70 | | - Code = DefaultExtendErrorCode; |
71 | | - Flag = EmptyFlag; |
72 | | - } |
73 | | - |
74 | | - /// <summary> |
75 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
76 | | - /// </summary> |
77 | | - /// <param name="errorCode">错误码</param> |
78 | | - /// <param name="errorMessage">错误消息</param> |
79 | | - /// <param name="flag">错误标识</param> |
80 | | - /// <param name="innerException">内部异常</param> |
81 | | - protected BingException(long errorCode, string errorMessage, string flag, Exception innerException = null) : base(errorMessage, innerException) |
82 | | - { |
83 | | - if (string.IsNullOrWhiteSpace(flag)) |
84 | | - flag = EmptyFlag; |
85 | | - ExtraData = new Dictionary<string, object>(); |
86 | | - Code = errorCode; |
87 | | - Flag = flag; |
88 | | - } |
89 | | - |
90 | | - /// <summary> |
91 | | - /// 初始化一个<see cref="BingException"/>类型的实例 |
92 | | - /// </summary> |
93 | | - /// <param name="options">Bing框架异常选项配置</param> |
94 | | - protected BingException(BingExceptionOptions options) : base(options.Message, options.InnerException) |
95 | | - { |
96 | | - ExtraData = options.ExtraErrors; |
97 | | - Code = options.ErrorCode; |
98 | | - Flag = options.Flag; |
99 | | - } |
100 | | - |
101 | | - /// <summary> |
102 | | - /// 错误码 |
103 | | - /// </summary> |
104 | | - public long Code { get; protected set; } |
105 | | - |
106 | | - /// <summary> |
107 | | - /// 错误标识 |
108 | | - /// </summary> |
109 | | - public string Flag { get; protected set; } |
110 | | - |
111 | | - /// <summary> |
112 | | - /// 额外数据 |
113 | | - /// </summary> |
114 | | - public Dictionary<string, object> ExtraData { get; } |
115 | | - |
116 | | - /// <summary> |
117 | | - /// 获取完整的消息 |
118 | | - /// </summary> |
119 | | - public virtual string GetFullMessage() => $"{Code}:({Flag}){Message}"; |
120 | | - |
121 | | - /// <summary> |
122 | | - /// 抛出异常 |
123 | | - /// </summary> |
124 | | - public virtual void Throw() => ExceptionHelper.PrepareForRethrow(this); |
| 67 | + ExtraData = new Dictionary<string, object>(); |
| 68 | + Code = DefaultExtendErrorCode; |
| 69 | + Flag = EmptyFlag; |
125 | 70 | } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 74 | + /// </summary> |
| 75 | + /// <param name="errorCode">错误码</param> |
| 76 | + /// <param name="errorMessage">错误消息</param> |
| 77 | + /// <param name="flag">错误标识</param> |
| 78 | + /// <param name="innerException">内部异常</param> |
| 79 | + protected BingException(long errorCode, string errorMessage, string flag, Exception innerException = null) : base(errorMessage, innerException) |
| 80 | + { |
| 81 | + if (string.IsNullOrWhiteSpace(flag)) |
| 82 | + flag = EmptyFlag; |
| 83 | + ExtraData = new Dictionary<string, object>(); |
| 84 | + Code = errorCode; |
| 85 | + Flag = flag; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// 初始化一个<see cref="BingException"/>类型的实例 |
| 90 | + /// </summary> |
| 91 | + /// <param name="options">Bing框架异常选项配置</param> |
| 92 | + protected BingException(BingExceptionOptions options) : base(options.Message, options.InnerException) |
| 93 | + { |
| 94 | + ExtraData = options.ExtraErrors; |
| 95 | + Code = options.ErrorCode; |
| 96 | + Flag = options.Flag; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// 错误码 |
| 101 | + /// </summary> |
| 102 | + public long Code { get; protected set; } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// 错误标识 |
| 106 | + /// </summary> |
| 107 | + public string Flag { get; protected set; } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// 额外数据 |
| 111 | + /// </summary> |
| 112 | + public Dictionary<string, object> ExtraData { get; } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// 获取完整的消息 |
| 116 | + /// </summary> |
| 117 | + public virtual string GetFullMessage() => $"{Code}:({Flag}){Message}"; |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// 抛出异常 |
| 121 | + /// </summary> |
| 122 | + public virtual void Throw() => ExceptionHelper.PrepareForRethrow(this); |
126 | 123 | } |
0 commit comments