Skip to content

Commit e9922e4

Browse files
Commiting 1.7
1 parent 15f999c commit e9922e4

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OneBitSoftware.Utilities.Errors;
2+
3+
public interface IOperationError
4+
{
5+
int? Code { get; set; }
6+
7+
string Message { get; set; }
8+
9+
string? Details { get; set; }
10+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
using System.Text;
44

5-
public class OperationError
5+
public class OperationError : IOperationError
66
{
7-
public OperationError(string message, int? code = null)
7+
public OperationError(string message, int? code = null, string? details = null)
88
{
99
this.Message = message;
1010
this.Code = code;
11+
this.Details = details;
1112
}
1213

1314
public int? Code { get; set; }

src/OneBitSoftware.Utilities.OperationResult/OneBitSoftware.Utilities.OperationResult.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
</ItemGroup>
1919

2020
<PropertyGroup>
21-
<PackageId>OneBitSoftware.Utilities.OperationResult</PackageId>
21+
<Authors>OneBit Software</Authors>
22+
<PackageId>OneBitSoftware.Utilities.OperationResult</PackageId>
2223
<RepositoryType>git</RepositoryType>
2324
<RepositoryUrl>https://github.com/OneBitSoftware/OperationResult</RepositoryUrl>
2425
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
@@ -31,7 +32,7 @@
3132
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
3233
<PackageReadmeFile>README.md</PackageReadmeFile>
3334
<PackageTags>OneBitSoftware; OperationResult;</PackageTags>
34-
<Version>1.1.6</Version>
35+
<Version>1.1.7</Version>
3536
</PropertyGroup>
3637

3738
</Project>

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// </summary>
1313
public class OperationResult
1414
{
15-
private readonly List<OperationError> _errors = new();
15+
private readonly List<IOperationError> _errors = new();
1616
private readonly List<string> _successMessages = new();
1717

1818
private readonly ILogger? _logger;
@@ -41,7 +41,7 @@ public IEnumerable<string> SuccessMessages
4141
/// <summary>
4242
/// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />.
4343
/// </summary>
44-
public IReadOnlyCollection<OperationError> Errors => this._errors.AsReadOnly();
44+
public IReadOnlyCollection<IOperationError> Errors => this._errors.AsReadOnly();
4545

4646
/// <summary>
4747
/// Gets or sets the first exception that resulted from the operation.
@@ -97,7 +97,7 @@ public OperationResult AppendErrors(OperationResult otherOperationResult)
9797
}
9898

9999
/// <summary>
100-
/// This method will append an error with a specific `user-friendly` message to this operation result instance.
100+
/// This method will append an <see cref="OperationError"/> error with a specific `user-friendly` message to this operation result instance.
101101
/// </summary>
102102
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
103103
/// <param name="code">The unique code of the error.</param>
@@ -115,12 +115,32 @@ public OperationResult AppendError(string message, int? code = null, LogLevel? l
115115
}
116116

117117
/// <summary>
118-
/// Appends an <see cref="OperationError"/> to the internal errors collection.
118+
/// This method will append an <typeparamref name="T"/> error with a specific `user-friendly` message to this operation result instance.
119+
/// </summary>
120+
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
121+
/// <param name="code">The unique code of the error.</param>
122+
/// <param name="logLevel">The logging severity.</param>
123+
/// <param name="details">A <see cref="string"/> with error details.</param>
124+
/// <typeparam name="T">The type of <see cref="IOperationError"/> to append.</typeparam>
125+
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
126+
public OperationResult AppendError<T>(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
127+
where T : IOperationError, new()
128+
{
129+
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentNullException(nameof(message));
130+
131+
var error = new T() { Message = message, Code = code, Details = details };
132+
this.AppendError(error, logLevel);
133+
134+
return this;
135+
}
136+
137+
/// <summary>
138+
/// Appends an <see cref="IOperationError"/> to the internal errors collection.
119139
/// </summary>
120-
/// <param name="error">An instance of <see cref="OperationError"/> to add to the internal errors collection.</param>
140+
/// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
121141
/// <param name="logLevel">The logging level.</param>
122142
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
123-
public OperationResult AppendError(OperationError error, LogLevel? logLevel = LogLevel.Error)
143+
public OperationResult AppendError(IOperationError error, LogLevel? logLevel = LogLevel.Error)
124144
{
125145
this.AppendErrorInternal(error);
126146

@@ -191,10 +211,10 @@ public static OperationResult FromError(string message, int? code = null, LogLev
191211
private static LogLevel GetLogLevel(LogLevel? optionalLevel) => optionalLevel ?? LogLevel.Error;
192212

193213
/// <summary>
194-
/// Appends an <see cref="OperationError"/> to the internal errors collection.
214+
/// Appends an <see cref="IOperationError"/> to the internal errors collection.
195215
/// </summary>
196-
/// <param name="error">An instance of <see cref="OperationError"/> to add to the internal errors collection.</param>
197-
private void AppendErrorInternal(OperationError error) => this._errors.Add(error);
216+
/// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
217+
private void AppendErrorInternal(IOperationError error) => this._errors.Add(error);
198218
}
199219

200220
/// <summary>

0 commit comments

Comments
 (0)