This commit is contained in:
@@ -22,12 +22,15 @@ jobs:
|
|||||||
run: dotnet restore Railway/Railway.csproj
|
run: dotnet restore Railway/Railway.csproj
|
||||||
|
|
||||||
- name: Setup nuget source
|
- name: Setup nuget source
|
||||||
run: dotnet nuget add source --name gitea_registry https://gitea.jstdev.ru/api/packages/just/nuget/index.json
|
run: dotnet nuget add source --name gitea_registry ${{ vars.OUTPUT_NUGET_REGISTRY }}
|
||||||
|
|
||||||
- name: Create the package
|
- name: Create the package
|
||||||
env:
|
env:
|
||||||
RELEASE_VERSION: ${{ gitea.ref_name }}
|
RELEASE_VERSION: ${{ gitea.ref_name }}
|
||||||
run: dotnet pack --no-restore --configuration Release --output nupkgs Railway/Railway.csproj `echo $RELEASE_VERSION | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'`
|
run: >
|
||||||
|
dotnet pack --no-restore --configuration Release --output nupkgs Railway/Railway.csproj
|
||||||
|
/p:RepositoryUrl ${{ gitea.server_url }}/${{ gitea.repository }}/
|
||||||
|
`echo $RELEASE_VERSION | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'`
|
||||||
|
|
||||||
- name: Publish the package to Gitea
|
- name: Publish the package to Gitea
|
||||||
run: dotnet nuget push --source gitea_registry --api-key ${{ secrets.NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg
|
run: dotnet nuget push --source gitea_registry --api-key ${{ secrets.NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Runtime.Serialization;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$$err")]
|
[JsonConverter(typeof(ErrorJsonConverter))]
|
||||||
[JsonDerivedType(typeof(ExpectedError), typeDiscriminator: 0)]
|
|
||||||
[JsonDerivedType(typeof(ExceptionalError), typeDiscriminator: 1)]
|
|
||||||
[JsonDerivedType(typeof(ManyErrors))]
|
|
||||||
public abstract class Error : IEquatable<Error>, IComparable<Error>
|
public abstract class Error : IEquatable<Error>, IComparable<Error>
|
||||||
{
|
{
|
||||||
protected internal Error(){}
|
protected internal Error(){}
|
||||||
@@ -33,16 +29,22 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">Error detail</param>
|
/// <param name="message">Error detail</param>
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error New(string message) =>
|
public static Error New(string message, IEnumerable<KeyValuePair<string, string>>? extensionData = null) =>
|
||||||
new ExpectedError("error", message);
|
new ExpectedError("error", message)
|
||||||
|
{
|
||||||
|
ExtensionData = extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty
|
||||||
|
};
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create an <see cref="ExpectedError"/>
|
/// Create an <see cref="ExpectedError"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">Error code</param>
|
/// <param name="type">Error code</param>
|
||||||
/// <param name="message">Error detail</param>
|
/// <param name="message">Error detail</param>
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error New(string type, string message) =>
|
public static Error New(string type, string message, IEnumerable<KeyValuePair<string, string>>? extensionData = null) =>
|
||||||
new ExpectedError(type, message);
|
new ExpectedError(type, message)
|
||||||
|
{
|
||||||
|
ExtensionData = extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty
|
||||||
|
};
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a <see cref="ManyErrors"/>
|
/// Create a <see cref="ManyErrors"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -50,7 +52,7 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
||||||
{
|
{
|
||||||
(null, null) => new ManyErrors(Enumerable.Empty<Error>()),
|
(null, null) => new ManyErrors([]),
|
||||||
(Error err, null) => err,
|
(Error err, null) => err,
|
||||||
(Error err, { IsEmpty: true }) => err,
|
(Error err, { IsEmpty: true }) => err,
|
||||||
(null, Error err) => err,
|
(null, Error err) => err,
|
||||||
@@ -76,13 +78,14 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
|
|
||||||
[Pure] public abstract string Type { get; }
|
[Pure] public abstract string Type { get; }
|
||||||
[Pure] public abstract string Message { get; }
|
[Pure] public abstract string Message { get; }
|
||||||
[Pure, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ImmutableDictionary<string, string>? ExtensionData { get; init; }
|
|
||||||
[Pure] public string? this[string key] => ExtensionData?.TryGetValue(key, out var value) == true ? value : null;
|
|
||||||
|
|
||||||
[Pure, JsonIgnore] public abstract int Count { get; }
|
[Pure] public ImmutableDictionary<string, string> ExtensionData { get; internal init; } = ImmutableDictionary<string, string>.Empty;
|
||||||
[Pure, JsonIgnore] public abstract bool IsEmpty { get; }
|
[Pure] public string? this[string key] => ExtensionData.TryGetValue(key, out var value) == true ? value : null;
|
||||||
[Pure, JsonIgnore] public abstract bool IsExpected { get; }
|
|
||||||
[Pure, JsonIgnore] public abstract bool IsExeptional { get; }
|
[Pure] public abstract int Count { get; }
|
||||||
|
[Pure] public abstract bool IsEmpty { get; }
|
||||||
|
[Pure] public abstract bool IsExpected { get; }
|
||||||
|
[Pure] public abstract bool IsExeptional { get; }
|
||||||
|
|
||||||
[Pure] public Error Append(Error? next)
|
[Pure] public Error Append(Error? next)
|
||||||
{
|
{
|
||||||
@@ -142,9 +145,9 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ExpectedErrorJsonConverter))]
|
||||||
public sealed class ExpectedError : Error
|
public sealed class ExpectedError : Error
|
||||||
{
|
{
|
||||||
[JsonConstructor]
|
|
||||||
public ExpectedError(string type, string message)
|
public ExpectedError(string type, string message)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = type;
|
||||||
@@ -158,10 +161,10 @@ public sealed class ExpectedError : Error
|
|||||||
[Pure] public override string Type { get; }
|
[Pure] public override string Type { get; }
|
||||||
[Pure] public override string Message { get; }
|
[Pure] public override string Message { get; }
|
||||||
|
|
||||||
[Pure, JsonIgnore] public override int Count => 1;
|
[Pure] public override int Count => 1;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => false;
|
[Pure] public override bool IsEmpty => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => true;
|
[Pure] public override bool IsExpected => true;
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => false;
|
[Pure] public override bool IsExeptional => false;
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public override IEnumerable<Error> ToEnumerable()
|
public override IEnumerable<Error> ToEnumerable()
|
||||||
@@ -170,24 +173,24 @@ public sealed class ExpectedError : Error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ExceptionalErrorJsonConverter))]
|
||||||
public sealed class ExceptionalError : Error
|
public sealed class ExceptionalError : Error
|
||||||
{
|
{
|
||||||
internal readonly Exception? Exception;
|
internal readonly Exception? Exception;
|
||||||
|
|
||||||
internal ExceptionalError(Exception exception)
|
internal ExceptionalError(Exception exception)
|
||||||
: this(exception.GetType().Name, exception.Message)
|
: this(exception.GetType().FullName ?? exception.GetType().Name, exception.Message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
ExtensionData = ExtractExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
}
|
}
|
||||||
internal ExceptionalError(string message, Exception exception)
|
internal ExceptionalError(string message, Exception exception)
|
||||||
: this(exception.GetType().Name, message)
|
: this(exception.GetType().FullName ?? exception.GetType().Name, message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
ExtensionData = ExtractExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConstructor]
|
|
||||||
public ExceptionalError(string type, string message)
|
public ExceptionalError(string type, string message)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = type;
|
||||||
@@ -197,10 +200,10 @@ public sealed class ExceptionalError : Error
|
|||||||
[Pure] public override string Type { get; }
|
[Pure] public override string Type { get; }
|
||||||
[Pure] public override string Message { get; }
|
[Pure] public override string Message { get; }
|
||||||
|
|
||||||
[Pure, JsonIgnore] public override int Count => 1;
|
[Pure] public override int Count => 1;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => false;
|
[Pure] public override bool IsEmpty => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => false;
|
[Pure] public override bool IsExpected => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => true;
|
[Pure] public override bool IsExeptional => true;
|
||||||
|
|
||||||
[Pure] public override Exception ToException() => Exception ?? base.ToException();
|
[Pure] public override Exception ToException() => Exception ?? base.ToException();
|
||||||
|
|
||||||
@@ -210,10 +213,10 @@ public sealed class ExceptionalError : Error
|
|||||||
yield return this;
|
yield return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ImmutableDictionary<string, string>? ExtractExtensionData(Exception exception)
|
private static ImmutableDictionary<string, string> ExtractExtensionData(Exception exception)
|
||||||
{
|
{
|
||||||
if (!(exception.Data?.Count > 0))
|
if (!(exception.Data?.Count > 0))
|
||||||
return null;
|
return ImmutableDictionary<string, string>.Empty;
|
||||||
|
|
||||||
List<KeyValuePair<string, string>>? values = null;
|
List<KeyValuePair<string, string>>? values = null;
|
||||||
|
|
||||||
@@ -231,16 +234,17 @@ public sealed class ExceptionalError : Error
|
|||||||
values ??= [];
|
values ??= [];
|
||||||
values.Add(new(keyString, valueString));
|
values.Add(new(keyString, valueString));
|
||||||
}
|
}
|
||||||
return values?.ToImmutableDictionary();
|
return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataContract]
|
[JsonConverter(typeof(ManyErrorsJsonConverter))]
|
||||||
public sealed class ManyErrors : Error, IEnumerable<Error>
|
public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
||||||
{
|
{
|
||||||
private readonly List<Error> _errors;
|
private readonly List<Error> _errors;
|
||||||
[Pure, DataMember] public IEnumerable<Error> Errors { get => _errors; }
|
[Pure] public IEnumerable<Error> Errors { get => _errors; }
|
||||||
|
|
||||||
|
internal ManyErrors(List<Error> errors) => _errors = errors;
|
||||||
internal ManyErrors(Error head, Error tail)
|
internal ManyErrors(Error head, Error tail)
|
||||||
{
|
{
|
||||||
_errors = new List<Error>(head.Count + tail.Count);
|
_errors = new List<Error>(head.Count + tail.Count);
|
||||||
@@ -283,9 +287,11 @@ public sealed class ManyErrors : Error, IEnumerable<Error>
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override int Count => _errors.Count;
|
[Pure] public override int Count => _errors.Count;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => _errors.Count == 0;
|
[Pure] public override bool IsEmpty => _errors.Count == 0;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
[Pure] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
[Pure] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
||||||
|
|
||||||
|
[Pure] public Error this[int index] => _errors[index];
|
||||||
|
|
||||||
[Pure] public override Exception ToException() => new AggregateException(_errors.Select(static x => x.ToException()));
|
[Pure] public override Exception ToException() => new AggregateException(_errors.Select(static x => x.ToException()));
|
||||||
[Pure] public override IEnumerable<Error> ToEnumerable() => _errors;
|
[Pure] public override IEnumerable<Error> ToEnumerable() => _errors;
|
||||||
|
|||||||
182
Railway/ErrorJsonConverter.cs
Normal file
182
Railway/ErrorJsonConverter.cs
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public sealed class ErrorJsonConverter : JsonConverter<Error>
|
||||||
|
{
|
||||||
|
public override Error? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ToExpectedError(ReadOne(ref reader)),
|
||||||
|
JsonTokenType.StartArray => ReadMany(ref reader),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, Error value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value is ManyErrors manyErrors)
|
||||||
|
{
|
||||||
|
writer.WriteStartArray();
|
||||||
|
foreach (var err in manyErrors)
|
||||||
|
{
|
||||||
|
WriteOne(writer, err);
|
||||||
|
}
|
||||||
|
writer.WriteEndArray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static ManyErrors ReadMany(ref Utf8JsonReader reader)
|
||||||
|
{
|
||||||
|
List<Error> errors = [];
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.StartObject)
|
||||||
|
{
|
||||||
|
errors.Add(ToExpectedError(ReadOne(ref reader)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ManyErrors(errors);
|
||||||
|
}
|
||||||
|
internal static ExpectedError ToExpectedError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
||||||
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
|
internal static (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) ReadOne(ref Utf8JsonReader reader)
|
||||||
|
{
|
||||||
|
List<KeyValuePair<string, string>>? extensionData = null;
|
||||||
|
string type = "error";
|
||||||
|
string message = "";
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
switch (reader.TokenType)
|
||||||
|
{
|
||||||
|
case JsonTokenType.PropertyName:
|
||||||
|
{
|
||||||
|
var propname = reader.GetString();
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
if (reader.TokenType == JsonTokenType.Null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
while (reader.TokenType == JsonTokenType.Comment) reader.Read();
|
||||||
|
|
||||||
|
if (!(reader.TokenType == JsonTokenType.String))
|
||||||
|
throw new JsonException("Unable to deserialize Error type.");
|
||||||
|
|
||||||
|
var propvalue = reader.GetString();
|
||||||
|
if (string.IsNullOrEmpty(propvalue))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (propname == "type" || string.Equals(propname, "type", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
type = propvalue;
|
||||||
|
}
|
||||||
|
else if (propname == "msg" || string.Equals(propname, "msg", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
message = propvalue;
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrEmpty(propname))
|
||||||
|
{
|
||||||
|
extensionData ??= [];
|
||||||
|
extensionData.Add(new(propname, propvalue));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case JsonTokenType.Comment: break;
|
||||||
|
case JsonTokenType.EndObject: goto endLoop;
|
||||||
|
default: throw new JsonException("Unable to deserialize Error type.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
endLoop:
|
||||||
|
return (type, message, extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty);
|
||||||
|
}
|
||||||
|
internal static void WriteOne(Utf8JsonWriter writer, Error value)
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
|
||||||
|
writer.WriteString("type", value.Type);
|
||||||
|
writer.WriteString("msg", value.Message);
|
||||||
|
|
||||||
|
if (value.ExtensionData?.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var (key, val) in value.ExtensionData)
|
||||||
|
{
|
||||||
|
writer.WriteString(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ExpectedErrorJsonConverter : JsonConverter<ExpectedError>
|
||||||
|
{
|
||||||
|
public override ExpectedError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ErrorJsonConverter.ToExpectedError(ErrorJsonConverter.ReadOne(ref reader)),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ExpectedError value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ExceptionalErrorJsonConverter : JsonConverter<ExceptionalError>
|
||||||
|
{
|
||||||
|
public override ExceptionalError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ToExceptionalError(ErrorJsonConverter.ReadOne(ref reader)),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ExceptionalError value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ExceptionalError ToExceptionalError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
||||||
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ManyErrorsJsonConverter : JsonConverter<ManyErrors>
|
||||||
|
{
|
||||||
|
public override ManyErrors? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartArray => ErrorJsonConverter.ReadMany(ref reader),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ManyErrors value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStartArray();
|
||||||
|
foreach (var err in value)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, err);
|
||||||
|
}
|
||||||
|
writer.WriteEndArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,10 +7,11 @@
|
|||||||
<AssemblyName>Just.Railway</AssemblyName>
|
<AssemblyName>Just.Railway</AssemblyName>
|
||||||
<RootNamespace>Just.Railway</RootNamespace>
|
<RootNamespace>Just.Railway</RootNamespace>
|
||||||
|
|
||||||
|
<Description>Base for railway-oriented programming in .NET. Package includes Result object, Error class and most of the common extensions.</Description>
|
||||||
|
<PackageTags>railway-oriented;functional;result-pattern;result-object;error-handling</PackageTags>
|
||||||
<Authors>JustFixMe</Authors>
|
<Authors>JustFixMe</Authors>
|
||||||
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<RepositoryUrl>https://gitea.jstdev.ru/just/Just.Railway/</RepositoryUrl>
|
|
||||||
|
|
||||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||||
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
using System.Collections.Immutable;
|
|
||||||
|
|
||||||
namespace Railway.Tests.Errors;
|
namespace Railway.Tests.Errors;
|
||||||
|
|
||||||
public class Serialization
|
public class Serialization
|
||||||
@@ -8,35 +6,64 @@ public class Serialization
|
|||||||
public void WhenSerializingManyErrors()
|
public void WhenSerializingManyErrors()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
Error many_errors = new ManyErrors(new Error[]{
|
Error many_errors = new ManyErrors(
|
||||||
new ExpectedError("err1", "msg1"){
|
[
|
||||||
ExtensionData = ImmutableDictionary<string, string>.Empty
|
Error.New("err1", "msg1", new KeyValuePair<string, string>[]
|
||||||
.Add("ext", "ext_value"),
|
{
|
||||||
},
|
new("ext", "ext_value"),
|
||||||
new ExceptionalError(new Exception("msg2")),
|
}),
|
||||||
});
|
Error.New(new Exception("msg2")),
|
||||||
|
]);
|
||||||
// When
|
// When
|
||||||
var result = JsonSerializer.Serialize(many_errors);
|
var result = JsonSerializer.Serialize(many_errors);
|
||||||
// Then
|
// Then
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expected: "[{\"$$err\":0,\"Type\":\"err1\",\"Message\":\"msg1\",\"ExtensionData\":{\"ext\":\"ext_value\"}},{\"$$err\":1,\"Type\":\"Exception\",\"Message\":\"msg2\"}]",
|
expected: "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext\":\"ext_value\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]",
|
||||||
result);
|
result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WhenDeserializingManyErrors()
|
public void WhenDeserializingManyErrorsAsError()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var json = "[{\"$$err\":0,\"Type\":\"err1\",\"Message\":\"msg1\",\"ExtensionData\":{\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"}},{\"$$err\":1,\"Type\":\"Exception\",\"Message\":\"msg2\"}]";
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
||||||
// When
|
// When
|
||||||
var result = JsonSerializer.Deserialize<Error[]>(json);
|
var result = JsonSerializer.Deserialize<Error>(json);
|
||||||
// Then
|
// Then
|
||||||
Assert.True(result?.Length == 2);
|
Assert.IsType<ManyErrors>(result);
|
||||||
|
ManyErrors manyErrors = (ManyErrors)result;
|
||||||
|
|
||||||
|
Assert.True(manyErrors.Count == 2);
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expected: new ManyErrors(new Error[]{
|
expected: Error.Many(
|
||||||
new ExpectedError("err1", "msg1"),
|
Error.New("err1", "msg1"),
|
||||||
new ExceptionalError(new Exception("msg2")),
|
Error.New(new Exception("msg2"))
|
||||||
}),
|
).ToEnumerable(),
|
||||||
|
manyErrors
|
||||||
|
);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: "ext_value1",
|
||||||
|
manyErrors[0]["ext1"]);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: "ext_value2",
|
||||||
|
manyErrors[0]["ext2"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenDeserializingManyErrorsAsManyErrors()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
||||||
|
// When
|
||||||
|
var result = JsonSerializer.Deserialize<ManyErrors>(json);
|
||||||
|
// Then
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Count == 2);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: Error.Many(
|
||||||
|
Error.New("err1", "msg1"),
|
||||||
|
Error.New(new Exception("msg2"))
|
||||||
|
).ToEnumerable(),
|
||||||
result
|
result
|
||||||
);
|
);
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
|
|||||||
Reference in New Issue
Block a user