Skip to content

Commit 0c47c0c

Browse files
committed
chore: fix bad code smell
1 parent 7cec513 commit 0c47c0c

File tree

12 files changed

+213
-78
lines changed

12 files changed

+213
-78
lines changed

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions.Tests/MasaArgumentExceptionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void TestThrowIfNull()
2020
catch (MasaArgumentException ex)
2121
{
2222
Assert.AreEqual(Data.Constants.ErrorCode.NOT_NULL_VALIDATOR, ex.ErrorCode);
23-
Assert.AreEqual("'{0}' must not be empty.", ex.ErrorMessage);
23+
Assert.AreEqual("'{0}' must not be empty.", ex.GetErrorMessage());
2424
}
2525
}
2626
}

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions/MasaArgumentException.cs

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,50 @@ namespace System;
88
[Serializable]
99
public class MasaArgumentException : MasaException
1010
{
11-
protected string? ParamName { get; }
11+
private string? _paramName;
12+
protected string? ParamName => _paramName;
1213

13-
public MasaArgumentException(string message)
14-
: base(message)
14+
public MasaArgumentException(
15+
string message,
16+
LogLevel? logLevel = null)
17+
: base(message, logLevel)
1518
{
1619
}
1720

18-
public MasaArgumentException(string message, string paramName)
19-
: base(message)
21+
public MasaArgumentException(
22+
string message,
23+
string paramName,
24+
LogLevel? logLevel = null)
25+
: base(message, logLevel)
2026
{
21-
ParamName = paramName;
27+
_paramName = paramName;
2228
}
2329

24-
public MasaArgumentException(string? paramName, string errorCode, params object[] parameters)
25-
: this((Exception?)null, errorCode, parameters)
30+
public MasaArgumentException(
31+
string? paramName,
32+
string errorCode,
33+
LogLevel? logLevel = null,
34+
params object[] parameters)
35+
: this((Exception?)null, errorCode, logLevel, parameters)
2636
{
27-
ParamName = paramName;
37+
_paramName = paramName;
2838
}
2939

30-
public MasaArgumentException(Exception? innerException, string errorCode, params object[] parameters)
31-
: base(innerException, errorCode, parameters)
40+
public MasaArgumentException(
41+
Exception? innerException,
42+
string errorCode,
43+
LogLevel? logLevel = null,
44+
params object[] parameters)
45+
: base(innerException, errorCode, logLevel, parameters)
3246
{
3347
}
3448

35-
public MasaArgumentException(string message, Exception? innerException)
36-
: base(message, innerException)
49+
public MasaArgumentException(string message, Exception? innerException, LogLevel? logLevel = null)
50+
: base(message, innerException, logLevel)
3751
{
3852
}
3953

40-
public MasaArgumentException(SerializationInfo serializationInfo, StreamingContext context)
54+
protected MasaArgumentException(SerializationInfo serializationInfo, StreamingContext context)
4155
: base(serializationInfo, context)
4256
{
4357
}
@@ -79,6 +93,7 @@ public static void ThrowIfGreaterThan<T>(T argument,
7993
ThrowIf(argument.CompareTo(maxValue) > 0,
8094
paramName,
8195
Masa.BuildingBlocks.Data.Constants.ErrorCode.LESS_THAN_OR_EQUAL_VALIDATOR,
96+
null,
8297
maxValue);
8398
}
8499

@@ -89,6 +104,7 @@ public static void ThrowIfGreaterThanOrEqual<T>(T argument,
89104
ThrowIf(argument.CompareTo(maxValue) >= 0,
90105
paramName,
91106
Masa.BuildingBlocks.Data.Constants.ErrorCode.LESS_THAN_VALIDATOR,
107+
null,
92108
maxValue);
93109
}
94110

@@ -99,6 +115,7 @@ public static void ThrowIfLessThan<T>(T argument,
99115
ThrowIf(argument.CompareTo(minValue) < 0,
100116
paramName,
101117
Masa.BuildingBlocks.Data.Constants.ErrorCode.GREATER_THAN_OR_EQUAL_VALIDATOR,
118+
null,
102119
minValue);
103120
}
104121

@@ -109,6 +126,7 @@ public static void ThrowIfLessThanOrEqual<T>(T argument,
109126
ThrowIf(argument.CompareTo(minValue) <= 0,
110127
paramName,
111128
Masa.BuildingBlocks.Data.Constants.ErrorCode.GREATER_THAN_VALIDATOR,
129+
null,
112130
minValue);
113131
}
114132

@@ -120,6 +138,7 @@ public static void ThrowIfOutOfRange<T>(T argument,
120138
ThrowIf(argument.CompareTo(minValue) < 0 || argument.CompareTo(maxValue) > 0,
121139
paramName,
122140
Masa.BuildingBlocks.Data.Constants.ErrorCode.OUT_OF_RANGE_VALIDATOR,
141+
null,
123142
minValue,
124143
maxValue);
125144
}
@@ -141,19 +160,36 @@ public static void ThrowIfContain(string? argument,
141160
);
142161
}
143162

144-
public static void ThrowIf(bool condition, string? paramName, string errorCode, params object[] parameters)
163+
public static void ThrowIf(
164+
bool condition,
165+
string? paramName,
166+
string errorCode,
167+
LogLevel? logLevel = null,
168+
params object[] parameters)
145169
{
146-
if (condition) Throw(paramName, errorCode, Masa.BuildingBlocks.Data.Constants.ErrorCode.GetErrorMessage(errorCode), parameters);
170+
if (condition)
171+
Throw(paramName, errorCode, Masa.BuildingBlocks.Data.Constants.ErrorCode.GetErrorMessage(errorCode), logLevel, parameters);
147172
}
148173

149-
public static void ThrowIf(bool condition, string? paramName, string errorCode, string? errorMessage, params object[] parameters)
174+
public static void ThrowIf(
175+
bool condition,
176+
string? paramName,
177+
string errorCode,
178+
string? errorMessage,
179+
LogLevel? logLevel = null,
180+
params object[] parameters)
150181
{
151-
if (condition) Throw(paramName, errorCode, errorMessage, parameters);
182+
if (condition) Throw(paramName, errorCode, errorMessage, logLevel, parameters);
152183
}
153184

154185
[DoesNotReturn]
155-
private static void Throw(string? paramName, string errorCode, string? errorMessage, params object[] parameters) =>
156-
throw new MasaArgumentException(paramName, errorCode, parameters)
186+
private static void Throw(
187+
string? paramName,
188+
string errorCode,
189+
string? errorMessage,
190+
LogLevel? logLevel,
191+
params object[] parameters) =>
192+
throw new MasaArgumentException(paramName, errorCode, logLevel, parameters)
157193
{
158194
ErrorMessage = errorMessage
159195
};

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions/MasaException.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ namespace System;
88
[Serializable]
99
public class MasaException : Exception
1010
{
11-
public virtual LogLevel? LogLevel { get; set; }
11+
private LogLevel? _logLevel;
12+
public LogLevel? LogLevel => _logLevel;
1213

13-
public string? ErrorCode { get; private set; }
14+
private string? _errorCode;
15+
public string? ErrorCode => _errorCode;
1416

1517
/// <summary>
1618
/// Provides error message that I18N is not used
1719
/// </summary>
18-
public string? ErrorMessage { get; set; }
20+
protected string? ErrorMessage { get; set; }
1921

20-
public object[] Parameters { get; private set; }
22+
private object[] _parameters;
23+
24+
public object[] Parameters => _parameters;
2125

2226
private bool _initialize;
2327

@@ -74,29 +78,32 @@ public MasaException()
7478
{
7579
}
7680

77-
public MasaException(string message)
81+
public MasaException(string message, LogLevel? logLevel = null)
7882
: base(message)
7983
{
84+
_logLevel = logLevel;
8085
}
8186

82-
public MasaException(string errorCode, params object[] parameters)
83-
: this(null, errorCode, parameters)
87+
public MasaException(string errorCode, LogLevel? logLevel = null, params object[] parameters)
88+
: this(null, errorCode, logLevel, parameters)
8489
{
8590
}
8691

87-
public MasaException(Exception? innerException, string errorCode, params object[] parameters)
92+
public MasaException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
8893
: base(null, innerException)
8994
{
90-
ErrorCode = errorCode;
91-
Parameters = parameters;
95+
_errorCode = errorCode;
96+
_parameters = parameters;
97+
_logLevel = logLevel;
9298
}
9399

94-
public MasaException(string message, Exception? innerException)
100+
public MasaException(string message, Exception? innerException, LogLevel? logLevel = null)
95101
: base(message, innerException)
96102
{
103+
_logLevel = logLevel;
97104
}
98105

99-
public MasaException(SerializationInfo serializationInfo, StreamingContext context)
106+
protected MasaException(SerializationInfo serializationInfo, StreamingContext context)
100107
: base(serializationInfo, context)
101108
{
102109
}
@@ -129,4 +136,6 @@ protected virtual string GetLocalizedMessageExecuting()
129136
}
130137

131138
protected virtual object[] GetParameters() => Parameters;
139+
140+
public string? GetErrorMessage() => ErrorMessage;
132141
}

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions/MasaValidatorException.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,40 @@ namespace System;
88
[Serializable]
99
public class MasaValidatorException : MasaArgumentException
1010
{
11-
public MasaValidatorException(string message)
12-
: base(message)
11+
public MasaValidatorException(string message, LogLevel? logLevel = null)
12+
: base(message, logLevel)
1313
{
1414
}
1515

16-
public MasaValidatorException(string? paramName, string errorCode, params object[] parameters)
17-
: base(paramName, errorCode, parameters)
16+
public MasaValidatorException(string? paramName, string errorCode, LogLevel? logLevel = null, params object[] parameters)
17+
: base(paramName, errorCode, logLevel, parameters)
1818
{
1919

2020
}
2121

22-
public MasaValidatorException(Exception? innerException, string errorCode, params object[] parameters)
23-
: base(innerException, errorCode, parameters)
22+
public MasaValidatorException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
23+
: base(innerException, errorCode, logLevel, parameters)
2424
{
2525
}
2626

2727
public MasaValidatorException(params ValidationModel[] validationModels)
28-
: base(FormatMessage(validationModels))
28+
: base(FormatMessage(validationModels), Microsoft.Extensions.Logging.LogLevel.Error)
2929
{
3030

3131
}
3232

33-
public MasaValidatorException(string message, Exception? innerException)
34-
: base(message, innerException)
33+
public MasaValidatorException(LogLevel? logLevel = null, params ValidationModel[] validationModels)
34+
: base(FormatMessage(validationModels), logLevel)
35+
{
36+
37+
}
38+
39+
public MasaValidatorException(string message, Exception? innerException, LogLevel? logLevel = null)
40+
: base(message, innerException, logLevel)
3541
{
3642
}
3743

38-
public MasaValidatorException(SerializationInfo serializationInfo, StreamingContext context)
44+
protected MasaValidatorException(SerializationInfo serializationInfo, StreamingContext context)
3945
: base(serializationInfo, context)
4046
{
4147
}
@@ -97,6 +103,7 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
97103
ThrowIf(argument.CompareTo(maxValue) > 0,
98104
paramName,
99105
Masa.BuildingBlocks.Data.Constants.ErrorCode.LESS_THAN_OR_EQUAL_VALIDATOR,
106+
null,
100107
maxValue);
101108
}
102109

@@ -107,6 +114,7 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
107114
ThrowIf(argument.CompareTo(maxValue) >= 0,
108115
paramName,
109116
Masa.BuildingBlocks.Data.Constants.ErrorCode.LESS_THAN_VALIDATOR,
117+
null,
110118
maxValue);
111119
}
112120

@@ -117,6 +125,7 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
117125
ThrowIf(argument.CompareTo(minValue) < 0,
118126
paramName,
119127
Masa.BuildingBlocks.Data.Constants.ErrorCode.GREATER_THAN_OR_EQUAL_VALIDATOR,
128+
null,
120129
minValue);
121130
}
122131

@@ -127,6 +136,7 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
127136
ThrowIf(argument.CompareTo(minValue) <= 0,
128137
paramName,
129138
Masa.BuildingBlocks.Data.Constants.ErrorCode.GREATER_THAN_VALIDATOR,
139+
null,
130140
minValue);
131141
}
132142

@@ -138,6 +148,7 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
138148
ThrowIf(argument.CompareTo(minValue) < 0 || argument.CompareTo(maxValue) > 0,
139149
paramName,
140150
Masa.BuildingBlocks.Data.Constants.ErrorCode.OUT_OF_RANGE_VALIDATOR,
151+
null,
141152
minValue,
142153
maxValue);
143154
}
@@ -163,28 +174,32 @@ private static string FormatMessage(IEnumerable<ValidationModel> models)
163174
bool condition,
164175
string? paramName,
165176
string errorCode,
177+
LogLevel? logLevel = null,
166178
params object[] parameters)
167179
{
168-
if (condition) Throw(paramName, errorCode, Masa.BuildingBlocks.Data.Constants.ErrorCode.GetErrorMessage(errorCode), parameters);
180+
if (condition)
181+
Throw(paramName, errorCode, Masa.BuildingBlocks.Data.Constants.ErrorCode.GetErrorMessage(errorCode), logLevel, parameters);
169182
}
170183

171184
public new static void ThrowIf(
172185
bool condition,
173186
string? paramName,
174187
string errorCode,
175188
string? errorMessage,
189+
LogLevel? logLevel = null,
176190
params object[] parameters)
177191
{
178-
if (condition) Throw(paramName, errorCode, errorMessage, parameters);
192+
if (condition) Throw(paramName, errorCode, errorMessage, logLevel, parameters);
179193
}
180194

181195
[DoesNotReturn]
182196
private static void Throw(
183197
string? paramName,
184198
string errorCode,
185199
string? errorMessage,
200+
LogLevel? logLevel,
186201
params object[] parameters)
187-
=> throw new MasaValidatorException(paramName, errorCode, parameters)
202+
=> throw new MasaValidatorException(paramName, errorCode, logLevel, parameters)
188203
{
189204
ErrorMessage = errorMessage
190205
};

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions/UserFriendlyException.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ public UserFriendlyException(string message, Exception? innerException)
1818
{
1919
}
2020

21-
public UserFriendlyException(string errorCode, string errorMessage, params object[] parameters)
22-
: base(errorCode, errorMessage, parameters)
21+
public UserFriendlyException(
22+
string errorCode,
23+
LogLevel? logLevel = null,
24+
params object[] parameters)
25+
: base(errorCode, logLevel, parameters)
2326
{
2427
}
2528

26-
public UserFriendlyException(Exception? innerException, string errorCode, string errorMessage, params object[] parameters)
27-
: base(innerException, errorCode, errorMessage, parameters)
29+
public UserFriendlyException(
30+
Exception? innerException,
31+
string errorCode,
32+
LogLevel? logLevel = null,
33+
params object[] parameters)
34+
: base(innerException, errorCode, logLevel, parameters)
2835
{
2936
}
3037
}

src/BuildingBlocks/Globalization/Masa.BuildingBlocks.Globalization.I18N/I18nOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.Globalization.I18N;
55

6-
public class I18N<TResourceSource> : II18N<TResourceSource> where TResourceSource : class
6+
public class I18N<TResourceSource> : II18N<TResourceSource>
77
{
88
private readonly I18NResource? _resource;
99
private readonly List<I18NResource?> _baseResources;

src/BuildingBlocks/Globalization/Masa.BuildingBlocks.Globalization.I18N/II18NOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.BuildingBlocks.Globalization.I18N;
55

6-
public interface II18N<TResourceSource> : II18N where TResourceSource : class
6+
public interface II18N<out TResourceSource> : II18N
77
{
88

99
}

0 commit comments

Comments
 (0)