Skip to content

Commit 3d7c01e

Browse files
committed
调整使用 .NET 6/7 新特性
1 parent fa41bbf commit 3d7c01e

27 files changed

+279
-453
lines changed

src/Cosmos.Core/Cosmos/Exceptions/ExceptionHelper.cs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Runtime.ExceptionServices;
2-
using System.Text;
32
using Cosmos.Reflection;
43

54
namespace Cosmos.Exceptions;
@@ -30,35 +29,31 @@ public static Exception PrepareForRethrow(Exception exception)
3029
/// Unwrap<br />
3130
/// 解包
3231
/// </summary>
33-
/// <param name="ex"></param>
32+
/// <param name="exception"></param>
3433
/// <returns></returns>
35-
public static Exception Unwrap(Exception ex)
34+
public static Exception Unwrap(Exception exception)
3635
{
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;
4240
}
4341

4442
/// <summary>
4543
/// Unwrap<br />
4644
/// 解包
4745
/// </summary>
48-
/// <param name="ex"></param>
46+
/// <param name="exception"></param>
4947
/// <param name="untilType"></param>
5048
/// <param name="mayDerivedClass"></param>
5149
/// <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)
5351
{
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);
5854
if (!untilType.IsSubclassOf(typeof(Exception)))
5955
throw new ArgumentException($"Type '{untilType}' does not be divided from {typeof(Exception)}", nameof(untilType));
6056

61-
var exception = ex;
6257
return exception.GetType() == untilType || mayDerivedClass && exception.GetType().IsSubclassOf(untilType)
6358
? exception
6459
: exception.InnerException is null
@@ -71,13 +66,12 @@ public static Exception Unwrap(Exception ex, Type untilType, bool mayDerivedClas
7166
/// 解包
7267
/// </summary>
7368
/// <typeparam name="TException"></typeparam>
74-
/// <param name="ex"></param>
69+
/// <param name="exception"></param>
7570
/// <returns></returns>
7671
[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
7973
{
80-
return ex.Unwrap(Types.Of<TException>());
74+
return exception.Unwrap(Types.Of<TException>());
8175
}
8276
}
8377

@@ -91,63 +85,71 @@ public static class ExceptionHelperExtensions
9185
/// Unwrap<br />
9286
/// 解包
9387
/// </summary>
94-
/// <param name="ex"></param>
88+
/// <param name="exception"></param>
9589
/// <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+
}
9894

9995
/// <summary>
10096
/// Unwrap<br />
10197
/// 解包
10298
/// </summary>
103-
/// <param name="ex"></param>
99+
/// <param name="exception"></param>
104100
/// <param name="untilType"></param>
105101
/// <param name="mayDerivedClass"></param>
106102
/// <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+
}
109107

110108
/// <summary>
111109
/// Unwrap<br />
112110
/// 解包
113111
/// </summary>
114112
/// <typeparam name="TException"></typeparam>
115-
/// <param name="ex"></param>
113+
/// <param name="exception"></param>
116114
/// <returns></returns>
117115
[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+
}
121120

122121
/// <summary>
123122
/// Unwrap and gets message<br />
124123
/// 解包并返回消息
125124
/// </summary>
126-
/// <param name="ex"></param>
125+
/// <param name="exception"></param>
127126
/// <returns></returns>
128127
[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+
}
130132

131133
/// <summary>
132134
/// Unwrap and gets full message<br />
133135
/// 解包,尝试返回完整信息
134136
/// </summary>
135-
/// <param name="ex"></param>
137+
/// <param name="exception"></param>
136138
/// <returns></returns>
137-
public static string ToFullUnwrappedString(this Exception ex)
139+
public static string ToFullUnwrappedString(this Exception exception)
138140
{
139141
var sb = new StringBuilder();
140-
if (ex is CosmosException cosmosException)
142+
if (exception is CosmosException cosmosException)
141143
{
142144
sb.AppendLine(cosmosException.GetFullMessage());
143-
if (ex.InnerException != null)
145+
if (exception.InnerException != null)
144146
{
145-
sb.Append(ex.InnerException.ToUnwrappedString());
147+
sb.Append(exception.InnerException.ToUnwrappedString());
146148
}
147149
}
148150
else
149151
{
150-
sb.Append(ex.ToUnwrappedString());
152+
sb.Append(exception.ToUnwrappedString());
151153
}
152154

153155
return sb.ToString();

0 commit comments

Comments
 (0)