Skip to content

Commit 35cc522

Browse files
Added OperationError
1 parent d39e579 commit 35cc522

3 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/OneBitSoftware.Utilities.OperationResult/Error.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace OneBitSoftware.Utilities.OperationResult.Errors;
2+
3+
using System.Text;
4+
5+
public class OperationError
6+
{
7+
public OperationError(string message)
8+
{
9+
this.Message = message;
10+
}
11+
12+
public OperationError(string message, int code)
13+
{
14+
this.Message = message;
15+
this.Code = code;
16+
}
17+
18+
public int? Code { get; set; }
19+
20+
public string Message { get; set; }
21+
22+
public string? Details { get; set; }
23+
24+
public override string ToString()
25+
{
26+
var result = new StringBuilder();
27+
28+
if (this.Code != null) result.AppendLine($"Code: {this.Code}");
29+
30+
if (!string.IsNullOrWhiteSpace(this.Message)) result.AppendLine($"Message: {this.Message}");
31+
32+
if (!string.IsNullOrWhiteSpace(this.Details)) result.AppendLine($"Trace: {this.Details}");
33+
34+
return result.ToString();
35+
}
36+
}

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace OneBitSoftware.Utilities.OperationResult
22
{
33
using Microsoft.Extensions.Logging;
4+
using OneBitSoftware.Utilities.OperationResult.Errors;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -10,7 +11,7 @@
1011
/// </summary>
1112
public class OperationResult
1213
{
13-
private readonly List<Error> _errors = new();
14+
private readonly List<OperationError> _errors = new();
1415
private readonly ILogger? _logger;
1516

1617
/// <summary>
@@ -26,7 +27,7 @@ public class OperationResult
2627
/// <summary>
2728
/// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />.
2829
/// </summary>
29-
public IReadOnlyCollection<Error> Errors => this._errors.AsReadOnly();
30+
public IReadOnlyCollection<OperationError> Errors => this._errors.AsReadOnly();
3031

3132
/// <summary>
3233
/// Gets or sets the first exception that resulted from the operation.
@@ -71,7 +72,7 @@ public OperationResult AppendError(string message, int errorCode = 0, LogLevel?
7172
if (message is null) throw new ArgumentNullException(nameof(message));
7273
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentNullException(nameof(message));
7374

74-
var error = new Error { Message = message,Code = errorCode };
75+
var error = new OperationError(message, errorCode);
7576
this.AppendError(error, logLevel);
7677

7778
return this;
@@ -100,7 +101,7 @@ public OperationResult AppendException(Exception exception, int errorCode = 0, L
100101
// Append the exception as a first if it is not yet set.
101102
this.InitialException ??= exception;
102103

103-
var error = new Error { Message = exception.ToString(), Code = errorCode };
104+
var error = new OperationError(exception.ToString(), errorCode);
104105
this.AppendError(error, logLevel);
105106

106107
return this;
@@ -199,7 +200,7 @@ public void ValidateAny<T>(IEnumerable<T> value, string className, string method
199200

200201
private static LogLevel GetLogLevel(LogLevel? optionalLevel) => optionalLevel ?? LogLevel.Error;
201202

202-
private void AppendError(Error error, LogLevel? logLevel)
203+
private void AppendError(OperationError error, LogLevel? logLevel)
203204
{
204205
this.AppendError(error);
205206

@@ -209,7 +210,7 @@ private void AppendError(Error error, LogLevel? logLevel)
209210
}
210211
}
211212

212-
private void AppendError(Error error) => this._errors.Add(error);
213+
private void AppendError(OperationError error) => this._errors.Add(error);
213214
}
214215

215216
/// <summary>

0 commit comments

Comments
 (0)