Skip to content

Commit b993552

Browse files
committed
🧩 Refactor: Use exception builder instead of throw exceptions directly.
1 parent 1d6167e commit b993552

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

‎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/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)