This commit is contained in:
+40
-6
@@ -9,6 +9,12 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
||||
{
|
||||
protected internal Error(){}
|
||||
|
||||
/// <summary>
|
||||
/// A reusable singleton representing the absence of an error (null-object pattern).
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public static Error Empty => EmptyError.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Create an <see cref="ExceptionalError"/>
|
||||
/// </summary>
|
||||
@@ -99,11 +105,33 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
||||
[Pure] public abstract bool IsExpected { get; }
|
||||
[Pure] public abstract bool IsExceptional { get; }
|
||||
|
||||
/// <summary>
|
||||
/// A reusable singleton representing the absence of an error (null-object pattern).
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public static Error Empty => EmptyError.Instance;
|
||||
[Pure] public Error WithExtensionData(IEnumerable<KeyValuePair<string, string>> extensionData)
|
||||
{
|
||||
return this switch
|
||||
{
|
||||
{ IsEmpty: true } => Empty,
|
||||
ExpectedError expected => new ExpectedError(expected.Type, expected.Message)
|
||||
{
|
||||
ExtensionData = extensionData
|
||||
.UnionBy(expected.ExtensionData, x => x.Key)
|
||||
.ToImmutableDictionary(),
|
||||
},
|
||||
ExceptionalError exceptional => new ExceptionalError(exceptional.Type, exceptional.Message, exceptional.Exception)
|
||||
{
|
||||
ExtensionData = extensionData
|
||||
.UnionBy(exceptional.ExtensionData, x => x.Key)
|
||||
.ToImmutableDictionary(),
|
||||
},
|
||||
ManyErrors manyErrors => new ManyErrors(manyErrors.AccessUnsafe())
|
||||
{
|
||||
ExtensionData = extensionData
|
||||
.UnionBy(manyErrors.ExtensionData, x => x.Key)
|
||||
.ToImmutableDictionary(),
|
||||
},
|
||||
|
||||
_ => throw new NotImplementedException(), // should not reach this path
|
||||
};
|
||||
}
|
||||
|
||||
[Pure] public Error Append(Error? next)
|
||||
{
|
||||
@@ -179,7 +207,7 @@ public sealed class EmptyError : Error
|
||||
[Pure] public override bool IsExceptional => false;
|
||||
|
||||
[Pure]
|
||||
public override IEnumerable<Error> ToEnumerable() { yield break; }
|
||||
public override IEnumerable<Error> ToEnumerable() => Enumerable.Empty<Error>();
|
||||
|
||||
[Pure]
|
||||
public override Exception ToException() =>
|
||||
@@ -250,6 +278,11 @@ public sealed class ExceptionalError : Error
|
||||
Exception = exception;
|
||||
ExtensionData = ExtractExtensionData(exception);
|
||||
}
|
||||
internal ExceptionalError(string type, string message, Exception? exception)
|
||||
: this(type, message)
|
||||
{
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
public ExceptionalError(string type, string message)
|
||||
{
|
||||
@@ -457,6 +490,7 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
||||
errors.Add(error);
|
||||
}
|
||||
|
||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] internal ImmutableArray<Error> AccessUnsafe() => _errors;
|
||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] internal override Error AccessUnsafe(int position) => _errors[position];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user