1
1
using System . Runtime . ExceptionServices ;
2
- using System . Text ;
3
2
using Cosmos . Reflection ;
4
3
5
4
namespace Cosmos . Exceptions ;
@@ -30,35 +29,31 @@ public static Exception PrepareForRethrow(Exception exception)
30
29
/// Unwrap<br />
31
30
/// 解包
32
31
/// </summary>
33
- /// <param name="ex "></param>
32
+ /// <param name="exception "></param>
34
33
/// <returns></returns>
35
- public static Exception Unwrap ( Exception ex )
34
+ public static Exception Unwrap ( Exception exception )
36
35
{
37
- if ( ex is null )
38
- throw new ArgumentNullException ( nameof ( ex ) ) ;
39
- while ( ex . InnerException != null )
40
- ex = ex . InnerException ;
41
- return ex ;
36
+ ArgumentNullException . ThrowIfNull ( exception ) ;
37
+ while ( exception . InnerException != null )
38
+ exception = exception . InnerException ;
39
+ return exception ;
42
40
}
43
41
44
42
/// <summary>
45
43
/// Unwrap<br />
46
44
/// 解包
47
45
/// </summary>
48
- /// <param name="ex "></param>
46
+ /// <param name="exception "></param>
49
47
/// <param name="untilType"></param>
50
48
/// <param name="mayDerivedClass"></param>
51
49
/// <returns></returns>
52
- public static Exception Unwrap ( Exception ex , Type untilType , bool mayDerivedClass = true )
50
+ public static Exception Unwrap ( Exception exception , Type untilType , bool mayDerivedClass = true )
53
51
{
54
- if ( ex is null )
55
- throw new ArgumentNullException ( nameof ( ex ) ) ;
56
- if ( untilType is null )
57
- throw new ArgumentNullException ( nameof ( untilType ) ) ;
52
+ ArgumentNullException . ThrowIfNull ( exception ) ;
53
+ ArgumentNullException . ThrowIfNull ( untilType ) ;
58
54
if ( ! untilType . IsSubclassOf ( typeof ( Exception ) ) )
59
55
throw new ArgumentException ( $ "Type '{ untilType } ' does not be divided from { typeof ( Exception ) } ", nameof ( untilType ) ) ;
60
56
61
- var exception = ex ;
62
57
return exception . GetType ( ) == untilType || mayDerivedClass && exception . GetType ( ) . IsSubclassOf ( untilType )
63
58
? exception
64
59
: exception . InnerException is null
@@ -71,13 +66,12 @@ public static Exception Unwrap(Exception ex, Type untilType, bool mayDerivedClas
71
66
/// 解包
72
67
/// </summary>
73
68
/// <typeparam name="TException"></typeparam>
74
- /// <param name="ex "></param>
69
+ /// <param name="exception "></param>
75
70
/// <returns></returns>
76
71
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
77
- public static Exception Unwrap < TException > ( Exception ex )
78
- where TException : Exception
72
+ public static Exception Unwrap < TException > ( Exception exception ) where TException : Exception
79
73
{
80
- return ex . Unwrap ( Types . Of < TException > ( ) ) ;
74
+ return exception . Unwrap ( Types . Of < TException > ( ) ) ;
81
75
}
82
76
}
83
77
@@ -91,63 +85,71 @@ public static class ExceptionHelperExtensions
91
85
/// Unwrap<br />
92
86
/// 解包
93
87
/// </summary>
94
- /// <param name="ex "></param>
88
+ /// <param name="exception "></param>
95
89
/// <returns></returns>
96
- public static Exception Unwrap ( this Exception ex )
97
- => ExceptionHelper . Unwrap ( ex ) ;
90
+ public static Exception Unwrap ( this Exception exception )
91
+ {
92
+ return ExceptionHelper . Unwrap ( exception ) ;
93
+ }
98
94
99
95
/// <summary>
100
96
/// Unwrap<br />
101
97
/// 解包
102
98
/// </summary>
103
- /// <param name="ex "></param>
99
+ /// <param name="exception "></param>
104
100
/// <param name="untilType"></param>
105
101
/// <param name="mayDerivedClass"></param>
106
102
/// <returns></returns>
107
- public static Exception Unwrap ( this Exception ex , Type untilType , bool mayDerivedClass = true )
108
- => ExceptionHelper . Unwrap ( ex , untilType , mayDerivedClass ) ;
103
+ public static Exception Unwrap ( this Exception exception , Type untilType , bool mayDerivedClass = true )
104
+ {
105
+ return ExceptionHelper . Unwrap ( exception , untilType , mayDerivedClass ) ;
106
+ }
109
107
110
108
/// <summary>
111
109
/// Unwrap<br />
112
110
/// 解包
113
111
/// </summary>
114
112
/// <typeparam name="TException"></typeparam>
115
- /// <param name="ex "></param>
113
+ /// <param name="exception "></param>
116
114
/// <returns></returns>
117
115
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
118
- public static Exception Unwrap < TException > ( this Exception ex )
119
- where TException : Exception
120
- => ExceptionHelper . Unwrap < TException > ( ex ) ;
116
+ public static Exception Unwrap < TException > ( this Exception exception ) where TException : Exception
117
+ {
118
+ return ExceptionHelper . Unwrap < TException > ( exception ) ;
119
+ }
121
120
122
121
/// <summary>
123
122
/// Unwrap and gets message<br />
124
123
/// 解包并返回消息
125
124
/// </summary>
126
- /// <param name="ex "></param>
125
+ /// <param name="exception "></param>
127
126
/// <returns></returns>
128
127
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
129
- public static string ToUnwrappedString ( this Exception ex ) => ex . Unwrap ( ) . Message ;
128
+ public static string ToUnwrappedString ( this Exception exception )
129
+ {
130
+ return exception . Unwrap ( ) . Message ;
131
+ }
130
132
131
133
/// <summary>
132
134
/// Unwrap and gets full message<br />
133
135
/// 解包,尝试返回完整信息
134
136
/// </summary>
135
- /// <param name="ex "></param>
137
+ /// <param name="exception "></param>
136
138
/// <returns></returns>
137
- public static string ToFullUnwrappedString ( this Exception ex )
139
+ public static string ToFullUnwrappedString ( this Exception exception )
138
140
{
139
141
var sb = new StringBuilder ( ) ;
140
- if ( ex is CosmosException cosmosException )
142
+ if ( exception is CosmosException cosmosException )
141
143
{
142
144
sb . AppendLine ( cosmosException . GetFullMessage ( ) ) ;
143
- if ( ex . InnerException != null )
145
+ if ( exception . InnerException != null )
144
146
{
145
- sb . Append ( ex . InnerException . ToUnwrappedString ( ) ) ;
147
+ sb . Append ( exception . InnerException . ToUnwrappedString ( ) ) ;
146
148
}
147
149
}
148
150
else
149
151
{
150
- sb . Append ( ex . ToUnwrappedString ( ) ) ;
152
+ sb . Append ( exception . ToUnwrappedString ( ) ) ;
151
153
}
152
154
153
155
return sb . ToString ( ) ;
0 commit comments