Skip to content

Commit 4e085e7

Browse files
Merge pull request #1 from OneBitSoftware/tgancheva/minor-code-fixes
Tgancheva/Minor code fixes
2 parents d1a41ee + 767e59f commit 4e085e7

3 files changed

Lines changed: 28 additions & 41 deletions

File tree

src/OneBitSoftware.Utilities.OperationResult/Errors/OperationError.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44

55
public class OperationError
66
{
7-
public OperationError(string message)
8-
{
9-
this.Message = message;
10-
}
11-
12-
public OperationError(string message, int code)
7+
public OperationError(string message, int? code = null)
138
{
149
this.Message = message;
1510
this.Code = code;

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,39 @@ public OperationResult AppendErrors(OperationResult otherOperationResult)
9797
/// This method will append an error with a specific `user-friendly` message to this operation result instance.
9898
/// </summary>
9999
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
100-
/// <param name="errorCode">The unique code of the error.</param>
100+
/// <param name="code">The unique code of the error.</param>
101101
/// <param name="logLevel">The logging severity.</param>
102+
/// <param name="details">A <see cref="string"/> with error details.</param>
102103
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
103-
public OperationResult AppendError(string message, int errorCode = 0, LogLevel? logLevel = null)
104+
public OperationResult AppendError(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
104105
{
105-
if (message is null) throw new ArgumentNullException(nameof(message));
106106
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentNullException(nameof(message));
107107

108-
var error = new OperationError(message, errorCode);
108+
var error = new OperationError(message, code) { Details = details };
109109
this.AppendError(error, logLevel);
110110

111111
return this;
112112
}
113113

114114
/// <summary>
115-
/// Appends an error message to the operation result instance.
115+
/// Appends an <see cref="OperationError"/> to the internal errors collection.
116116
/// </summary>
117-
/// <param name="message">The message that should be appended.</param>
118-
/// <param name="errorCode">The unique code of the error.</param>
119-
/// <param name="logLevel">The logging severity.</param>
117+
/// <param name="error">An instance of <see cref="OperationError"/> to add to the internal errors collection.</param>
118+
/// <param name="logLevel">The logging level.</param>
120119
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
121-
public OperationResult AppendErrorMessage(string message, int errorCode = 0, LogLevel? logLevel = null) => this.AppendError(message, errorCode, logLevel);
120+
public OperationResult AppendError(OperationError error, LogLevel? logLevel = LogLevel.Error)
121+
{
122+
this.AppendErrorInternal(error);
123+
124+
if (this._logger is not null)
125+
{
126+
#pragma warning disable CA2254 // Template should be a static expression
127+
this._logger.Log(GetLogLevel(logLevel), error.Message);
128+
#pragma warning restore CA2254 // Template should be a static expression
129+
}
130+
131+
return this;
132+
}
122133

123134
/// <summary>
124135
/// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogEventLevel"/> level. A call to this method will set the Success property to false.
@@ -149,26 +160,6 @@ public OperationResult AppendException(Exception exception, int errorCode = 0, L
149160
// TODO: this method needs completing.
150161
private static LogLevel GetLogLevel(LogLevel? optionalLevel) => optionalLevel ?? LogLevel.Error;
151162

152-
/// <summary>
153-
/// Appends an <see cref="OperationError"/> to the internal errors collection.
154-
/// </summary>
155-
/// <param name="error">An instance of <see cref="OperationError"/> to add to the internal errors collection.</param>
156-
/// <param name="logLevel">The logging level.</param>
157-
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
158-
public OperationResult AppendError(OperationError error, LogLevel? logLevel = LogLevel.Error)
159-
{
160-
this.AppendErrorInternal(error);
161-
162-
if (this._logger is not null)
163-
{
164-
#pragma warning disable CA2254 // Template should be a static expression
165-
this._logger.Log(GetLogLevel(logLevel), error.Message);
166-
#pragma warning restore CA2254 // Template should be a static expression
167-
}
168-
169-
return this;
170-
}
171-
172163
/// <summary>
173164
/// Appends an <see cref="OperationError"/> to the internal errors collection.
174165
/// </summary>
@@ -229,12 +220,13 @@ public OperationResult(TResult resultObject) : base()
229220
/// This method will append an error with a specific `user-friendly` message to this operation result instance.
230221
/// </summary>
231222
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
232-
/// <param name="errorCode">The unique code of the error.</param>
223+
/// <param name="code">The unique code of the error.</param>
233224
/// <param name="logLevel">The logging severity.</param>
225+
/// <param name="details">A <see cref="string"/> with error details.</param>
234226
/// <returns>The current instance of the <see cref="OperationResult{TResult}"/>.</returns>
235-
public new OperationResult<TResult> AppendError(string message, int errorCode = 0, LogLevel? logLevel = null)
227+
public new OperationResult<TResult> AppendError(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
236228
{
237-
base.AppendError(message, errorCode, logLevel);
229+
base.AppendError(message, code, logLevel, details);
238230

239231
return this;
240232
}

src/OneBitSoftware.Utilities.OperationResult/OperationResultValidationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void ValidateDefault<T, TValue>(this OperationResult<T> operationR
5151
if (value.Equals(default) == false) return;
5252

5353
var errorMessage = $"{className}, {methodName} - The {propertyName} has a default value.";
54-
operationResult.AppendErrorMessage(errorMessage, logLevel: level);
54+
operationResult.AppendError(errorMessage, logLevel: level);
5555
}
5656

5757
/// <summary>
@@ -70,7 +70,7 @@ public static void ValidateNullOrWhitespace<T>(this OperationResult<T> operation
7070
if (string.IsNullOrWhiteSpace(value) == false) return;
7171

7272
var errorMessage = $"{className}, {methodName} - The {propertyName} is null, empty or consists only of whitespace characters.";
73-
operationResult.AppendErrorMessage(errorMessage, logLevel: level);
73+
operationResult.AppendError(errorMessage, logLevel: level);
7474
}
7575

7676
/// <summary>
@@ -91,6 +91,6 @@ public static void ValidateNull<T>(this OperationResult<T> operationResult, obje
9191
if (value != null) return;
9292

9393
var errorMessage = $"{className}, {methodName} - The {propertyName} is null.";
94-
operationResult.AppendErrorMessage(errorMessage, logLevel: level);
94+
operationResult.AppendError(errorMessage, logLevel: level);
9595
}
9696
}

0 commit comments

Comments
 (0)