Skip to content

Commit aa66377

Browse files
authored
Merge pull request #7 from Crequency/dev=main
[Pull Request] New error codes handler and exc
2 parents 6dc39a4 + 1690bbf commit aa66377

8 files changed

Lines changed: 129 additions & 13 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Common.BasicHelper.Utils.Extensions;
2+
3+
namespace Common.BasicHelper.Core.Exceptions;
4+
5+
[TestClass()]
6+
public class ErrorCodes_Tests
7+
{
8+
[TestMethod()]
9+
public void Test_GetExceptionMessage()
10+
{
11+
foreach (var item in Enum.GetValues(typeof(ErrorCodes)))
12+
{
13+
var ec = (ErrorCodes)item;
14+
$"{ec}\t{ec.BuildMessage()}".Print();
15+
}
16+
}
17+
}

Common.BasicHelper.Test/Utils/Extensions/StringHelper_Tests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ public async Task Test_ReadAllTextFromDiskAsync()
7878

7979
Assert.AreEqual(read, "Test");
8080
}
81+
82+
[TestMethod()]
83+
public void Test_Throw()
84+
{
85+
Assert.ThrowsException<ArgumentException>("Exception Message".Throw<ArgumentException>);
86+
}
8187
}

Common.BasicHelper.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.BasicHelper", "Commo
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.BasicHelper.Test", "Common.BasicHelper.Test\Common.BasicHelper.Test.csproj", "{D3DFF9AC-8CF1-42FF-BAF1-954A430DFDCE}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.BasicHelper.Samples", "Common.BasicHelper.Samples\Common.BasicHelper.Samples.csproj", "{C363C055-5CDC-4CEB-935C-D099CE0681BF}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.BasicHelper.Samples", "Common.BasicHelper.Samples\Common.BasicHelper.Samples.csproj", "{C363C055-5CDC-4CEB-935C-D099CE0681BF}"
1111
EndProject
1212
Global
1313
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace Common.BasicHelper.Core.Exceptions;
2+
3+
public enum ErrorCodes
4+
{
5+
Undefined,
6+
CB0017,
7+
CB0027,
8+
CB0033,
9+
CB0034,
10+
}
11+
12+
public class ErrorCodesHelper
13+
{
14+
public static string GetExceptionMessage
15+
(
16+
ErrorCodes ec,
17+
string? attachment = null,
18+
string? functionName = null,
19+
string? parameterName = null,
20+
string? inputFormatRequirements = null,
21+
string? inputLengthRequirements = null
22+
)
23+
=> ec switch
24+
{
25+
ErrorCodes.CB0017 => $"Only use `{functionName}` function for same types. {attachment}",
26+
ErrorCodes.CB0027 => $"Parameter `{parameterName}` not found. {attachment}",
27+
ErrorCodes.CB0033 => $"Error input format. {inputFormatRequirements} {attachment}",
28+
ErrorCodes.CB0034 => $"Error input length. {inputLengthRequirements} {attachment}",
29+
_ => $"Message for {ec} not found. {attachment}",
30+
};
31+
}
32+
33+
public static class ErrorCodesExtensions
34+
{
35+
public static string BuildMessage
36+
(
37+
this ErrorCodes ec,
38+
string? attachment = null,
39+
string? functionName = null,
40+
string? parameterName = null,
41+
string? inputFormatRequirements = null,
42+
string? inputLengthRequirements = null
43+
)
44+
=> ErrorCodesHelper.GetExceptionMessage
45+
(
46+
ec,
47+
attachment,
48+
functionName,
49+
parameterName,
50+
inputFormatRequirements,
51+
inputLengthRequirements
52+
);
53+
}

Common.BasicHelper/Graphics/Screen/Resolution.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Common.BasicHelper.Core.Exceptions;
2+
using Common.BasicHelper.Utils.Extensions;
3+
using System;
24
using System.Collections.Generic;
35

46
namespace Common.BasicHelper.Graphics.Screen;
@@ -180,7 +182,7 @@ public override string ToString() =>
180182
public override bool Equals(object obj)
181183
{
182184
if (obj is not Resolution)
183-
throw new ArgumentException($"CB0017: Only use `Equals` function for same type.");
185+
ErrorCodes.CB0017.BuildMessage(parameterName: nameof(Equals)).Throw<ArgumentException>();
184186

185187
return GetHashCode() == obj.GetHashCode();
186188

Common.BasicHelper/Utils/COID.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Common.BasicHelper.Math;
1+
using Common.BasicHelper.Core.Exceptions;
2+
using Common.BasicHelper.Math;
3+
using Common.BasicHelper.Utils.Extensions;
24
using System;
35
using System.Text.RegularExpressions;
46

@@ -107,7 +109,9 @@ public COID(string input, char sep = '-')
107109
var tmp = input.Split(sep);
108110

109111
if (tmp.Length != 5)
110-
throw new ArgumentException("CB0033: Error input format, it should be 5 parts.");
112+
ErrorCodes.CB0033
113+
.BuildMessage(inputFormatRequirements: "Need 5 parts.")
114+
.Throw<ArgumentException>();
111115

112116
for (int i = 0; i < 5; i++)
113117
parts[i] = new(tmp[i]);
@@ -170,13 +174,15 @@ public COID_Part(string input)
170174
ids = new char[5];
171175

172176
if (input.Length != 5)
173-
throw new ArgumentException("CB0034: Error input length, it should be 5.");
177+
ErrorCodes.CB0034
178+
.BuildMessage(inputLengthRequirements: "It should be 5 chars.")
179+
.Throw<ArgumentException>();
174180
else
175181
for (int i = 0; i < 5; i++)
176182
ids[i] = input[i];
177183

178184
if (!COID_Helper.FormatCheck(this))
179-
throw new FormatException("CB0035: Error COID_Part format.");
185+
ErrorCodes.CB0033.BuildMessage().Throw<FormatException>();
180186
}
181187

182188
/// <summary>

Common.BasicHelper/Utils/Extensions/StringHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,15 @@ void normalSeparate()
105105
/// <param name="str">字符串对象</param>
106106
/// <returns>是否为空或仅有空白组成</returns>
107107
public static bool IsNullOrWhiteSpace(this string? str) => string.IsNullOrWhiteSpace(str);
108+
109+
/// <summary>
110+
/// 将字符串作为异常消息抛出
111+
/// </summary>
112+
/// <typeparam name="T">异常类型</typeparam>
113+
/// <param name="message">异常消息</param>
114+
public static void Throw<T>(this string? message) where T : Exception
115+
{
116+
var exp = Activator.CreateInstance(typeof(T), message);
117+
throw (exp as T) ?? new Exception(message);
118+
}
108119
}

Common.BasicHelper/Utils/Password.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Common.BasicHelper.Core.Exceptions;
2+
using Common.BasicHelper.Utils.Extensions;
3+
using System;
24
using System.Linq;
35
using System.Text;
46

@@ -64,11 +66,30 @@ public static string GeneratePassword
6466
&& supportedNumbers.Length == 0
6567
&& supportedSymbols.Length == 0;
6668

67-
if (!lengthProvided) throw new ArgumentException("CB0027: No length provided.");
68-
69-
if (noCharIncluded) throw new ArgumentException("CB0028: At least one char type included.");
70-
71-
if (noSupportedChars) throw new ArgumentException("CB0029: No supported chars provided.");
69+
if (!lengthProvided)
70+
ErrorCodes.CB0027
71+
.BuildMessage(
72+
parameterName: "length | lengthRangeStart | lengthRangeEnd",
73+
attachment: "At least one type of length provided."
74+
).Throw<ArgumentException>();
75+
76+
if (noCharIncluded)
77+
ErrorCodes.CB0027
78+
.BuildMessage(
79+
parameterName: "includeUppercase | includeLowercase | includeNumbers | includeSymbols",
80+
attachment: "At least one element type included."
81+
).Throw<ArgumentException>();
82+
83+
if (noSupportedChars)
84+
ErrorCodes.CB0027
85+
.BuildMessage(
86+
parameterName: "" +
87+
"supportedUppercases | " +
88+
"supportedLowercases | " +
89+
"supportedNumbers | " +
90+
"supportedSymbols",
91+
attachment: "No supported chars provided."
92+
).Throw<ArgumentException>();
7293

7394
#endregion Guard Blocks
7495

0 commit comments

Comments
 (0)