Skip to content

Commit b4554f9

Browse files
committed
💾 ✔️ Feat, Test: ErrorCodes handler built.
1 parent 6dc39a4 commit b4554f9

2 files changed

Lines changed: 70 additions & 0 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+
}
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+
}

0 commit comments

Comments
 (0)