diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyException.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyException.cs new file mode 100644 index 0000000..246b925 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyException.cs @@ -0,0 +1,16 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions +{ + internal class TestDependencyException : Xeption, ISerializationDependencyException + { + public TestDependencyException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyValidationException.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyValidationException.cs new file mode 100644 index 0000000..f56dc90 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestDependencyValidationException.cs @@ -0,0 +1,16 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions +{ + internal class TestDependencyValidationException : Xeption, ISerializationDependencyValidationException + { + public TestDependencyValidationException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestServiceException.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestServiceException.cs new file mode 100644 index 0000000..e0de5e2 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestServiceException.cs @@ -0,0 +1,16 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions +{ + internal class TestServiceException : Xeption, ISerializationServiceException + { + public TestServiceException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestValidationException.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestValidationException.cs new file mode 100644 index 0000000..c09563b --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/Models/Exceptions/TestValidationException.cs @@ -0,0 +1,16 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions +{ + internal class TestValidationException : Xeption, ISerializationValidationException + { + public TestValidationException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/STX.Serialization.Providers.Abstractions.Tests.Unit.csproj b/STX.Serialization.Providers.Abstractions.Tests.Unit/STX.Serialization.Providers.Abstractions.Tests.Unit.csproj new file mode 100644 index 0000000..6ca9d95 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/STX.Serialization.Providers.Abstractions.Tests.Unit.csproj @@ -0,0 +1,31 @@ + + + + net8.0 + disable + disable + false + true + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Exceptions.Serialize.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Exceptions.Serialize.cs new file mode 100644 index 0000000..9912953 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Exceptions.Serialize.cs @@ -0,0 +1,231 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Threading.Tasks; +using FluentAssertions; +using Moq; +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions; +using Xeptions; +using Xunit; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit +{ + public partial class SerializationAbstractionProviderTests + { + [Fact] + public async Task ShouldCaptureAndLocaliseValidationExceptionsAsync() + { + // given + dynamic dynamicPerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = dynamicPerson; + Xeption someException = new Xeption(message: "Some exception occurred."); + + TestValidationException testValidationException = + new TestValidationException( + message: "Serialization validation errors occurred, please try again.", + innerException: someException); + + SerializationValidationProviderException expectedSerializationValidationProviderException = + new SerializationValidationProviderException( + message: "Serialization validation errors occurred, please try again.", + innerException: testValidationException, + data: testValidationException.Data); + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ThrowsAsync(testValidationException); + + // when + ValueTask serializationTask = + this.serializationAbstractionProvider.Serialize(inputPerson); + + SerializationValidationProviderException actualSerializationValidationProviderException = + await Assert.ThrowsAsync( + serializationTask.AsTask); + + // then + actualSerializationValidationProviderException.Should() + .BeEquivalentTo(expectedSerializationValidationProviderException); + } + + [Fact] + public async Task ShouldCaptureAndLocaliseDependencyValidationExceptionsAsync() + { + // given + dynamic dynamicPerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = dynamicPerson; + Xeption someException = new Xeption(message: "Some exception occurred."); + + TestDependencyValidationException testDependencyValidationException = + new TestDependencyValidationException( + message: "Serialization dependency validation errors occurred, please try again.", + innerException: someException); + + SerializationValidationProviderException expectedSerializationValidationProviderException = + new SerializationValidationProviderException( + message: "Serialization validation errors occurred, please try again.", + innerException: testDependencyValidationException, + data: testDependencyValidationException.Data); + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ThrowsAsync(testDependencyValidationException); + + // when + ValueTask serializationTask = + this.serializationAbstractionProvider.Serialize(inputPerson); + + SerializationValidationProviderException actualSerializationValidationProviderException = + await Assert.ThrowsAsync( + serializationTask.AsTask); + + // then + actualSerializationValidationProviderException.Should() + .BeEquivalentTo(expectedSerializationValidationProviderException); + } + + [Fact] + public async Task ShouldCaptureAndLocaliseDependencyExceptionsAsync() + { + // given + dynamic dynamicPerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = dynamicPerson; + Xeption someException = new Xeption(message: "Some exception occurred."); + + TestDependencyException testDependencyException = + new TestDependencyException( + message: "Serialization dependency error occurred, contact support.", + innerException: someException); + + SerializationDependencyProviderException expectedSerializationDependencyProviderException = + new SerializationDependencyProviderException( + message: "Serialization dependency error occurred, contact support.", + innerException: testDependencyException, + data: testDependencyException.Data); + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ThrowsAsync(testDependencyException); + + // when + ValueTask serializationTask = + this.serializationAbstractionProvider.Serialize(inputPerson); + + SerializationDependencyProviderException actualSerializationDependencyProviderException = + await Assert.ThrowsAsync( + serializationTask.AsTask); + + // then + actualSerializationDependencyProviderException.Should() + .BeEquivalentTo(expectedSerializationDependencyProviderException); + } + + [Fact] + public async Task ShouldCaptureAndLocaliseServiceExceptionsAsync() + { + // given + dynamic dynamicPerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = dynamicPerson; + Xeption someException = new Xeption(message: "Some exception occurred."); + + TestServiceException testServiceException = + new TestServiceException( + message: "Serialization service error occurred, contact support.", + innerException: someException); + + SerializationServiceProviderException expectedSerializationServiceProviderException = + new SerializationServiceProviderException( + message: "Serialization service error occurred, contact support.", + innerException: testServiceException, + data: testServiceException.Data); + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ThrowsAsync(testServiceException); + + // when + ValueTask serializationTask = + this.serializationAbstractionProvider.Serialize(inputPerson); + + SerializationServiceProviderException actualSerializationServiceProviderException = + await Assert.ThrowsAsync( + serializationTask.AsTask); + + // then + actualSerializationServiceProviderException.Should() + .BeEquivalentTo(expectedSerializationServiceProviderException); + } + + [Fact] + public async Task ShouldCaptureAndLocaliseAnyNonServiceExceptionsAsync() + { + // given + dynamic dynamicPerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = dynamicPerson; + Exception someException = new Exception(message: "Some exception occurred."); + + UncatagorizedSerializationProviderException notImplementedSerializationProviderException = + new UncatagorizedSerializationProviderException( + message: "Serialization provider not properly implemented. Uncatagorized errors found, " + + "contact the serialization provider owner for support.", + + innerException: someException, + data: someException.Data); + + SerializationServiceProviderException expectedSerializationServiceProviderException = + new SerializationServiceProviderException( + message: "Uncatagorized serialization service error occurred, contact support.", + innerException: notImplementedSerializationProviderException, + data: notImplementedSerializationProviderException.Data); + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ThrowsAsync(someException); + + // when + ValueTask serializationTask = + this.serializationAbstractionProvider.Serialize(inputPerson); + + SerializationServiceProviderException actualSerializationServiceProviderException = + await Assert.ThrowsAsync( + serializationTask.AsTask); + + // then + actualSerializationServiceProviderException.Should() + .BeEquivalentTo(expectedSerializationServiceProviderException); + } + } +} \ No newline at end of file diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Logic.Serialize.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Logic.Serialize.cs new file mode 100644 index 0000000..e951bc0 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.Logic.Serialize.cs @@ -0,0 +1,41 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Threading.Tasks; +using FluentAssertions; +using Moq; +using Xunit; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit +{ + public partial class SerializationAbstractionProviderTests + { + [Fact] + public async Task ShouldSerializeAsync() + { + // given + dynamic somePerson = new + { + Name = GetRandomString(), + Surname = GetRandomString(), + Age = GetRandomNumber() + }; + + dynamic inputPerson = somePerson; + string randomString = GetRandomString(); + string outputString = GetRandomString(); + string expectedString = outputString; + + this.serializationProviderMock.Setup(provider => + provider.Serialize(It.IsAny())) + .ReturnsAsync(outputString); + + // when + string actualString = await this.serializationAbstractionProvider.Serialize(inputPerson); + + // then + actualString.Should().BeEquivalentTo(expectedString); + } + } +} \ No newline at end of file diff --git a/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.cs b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.cs new file mode 100644 index 0000000..78d2d78 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions.Tests.Unit/SerializationAbstractionProviderTests.cs @@ -0,0 +1,29 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using Moq; +using Tynamix.ObjectFiller; + +namespace STX.Serialization.Providers.Abstractions.Tests.Unit +{ + public partial class SerializationAbstractionProviderTests + { + private readonly Mock serializationProviderMock; + private readonly ISerializationAbstractionProvider serializationAbstractionProvider; + + public SerializationAbstractionProviderTests() + { + this.serializationProviderMock = new Mock(); + + this.serializationAbstractionProvider = new SerializationAbstractionProvider( + serializationProvider: this.serializationProviderMock.Object); + } + + private static string GetRandomString() => + new MnemonicString(wordCount: GetRandomNumber()).GetValue(); + + private static int GetRandomNumber() => + new IntRange(min: 2, max: 10).GetValue(); + } +} \ No newline at end of file diff --git a/STX.Serialization.Providers.Abstractions.sln b/STX.Serialization.Providers.Abstractions.sln index 4248f07..a832930 100644 --- a/STX.Serialization.Providers.Abstractions.sln +++ b/STX.Serialization.Providers.Abstractions.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STX.Serialization.Providers EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STX.Serialization.Providers.Abstractions.Tests.Acceptance", "STX.Serialization.Providers.Abstractions.Tests.Acceptance\STX.Serialization.Providers.Abstractions.Tests.Acceptance.csproj", "{A3E3CBE6-74E2-4196-94E1-F4238C36639E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STX.Serialization.Providers.Abstractions.Tests.Unit", "STX.Serialization.Providers.Abstractions.Tests.Unit\STX.Serialization.Providers.Abstractions.Tests.Unit.csproj", "{270ECD52-7950-448E-BA0B-55C6A30117FD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {A3E3CBE6-74E2-4196-94E1-F4238C36639E}.Debug|Any CPU.Build.0 = Debug|Any CPU {A3E3CBE6-74E2-4196-94E1-F4238C36639E}.Release|Any CPU.ActiveCfg = Release|Any CPU {A3E3CBE6-74E2-4196-94E1-F4238C36639E}.Release|Any CPU.Build.0 = Release|Any CPU + {270ECD52-7950-448E-BA0B-55C6A30117FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {270ECD52-7950-448E-BA0B-55C6A30117FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {270ECD52-7950-448E-BA0B-55C6A30117FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {270ECD52-7950-448E-BA0B-55C6A30117FD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/STX.Serialization.Providers.Abstractions/ISerializationAbstractionProvider.cs b/STX.Serialization.Providers.Abstractions/ISerializationAbstractionProvider.cs new file mode 100644 index 0000000..2ddf876 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/ISerializationAbstractionProvider.cs @@ -0,0 +1,14 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Threading.Tasks; + +namespace STX.Serialization.Providers.Abstractions +{ + public interface ISerializationAbstractionProvider + { + ValueTask Serialize(T obj); + ValueTask Deserialize(string json); + } +} diff --git a/STX.Serialization.Providers.Abstractions/ISerializationProvider.cs b/STX.Serialization.Providers.Abstractions/ISerializationProvider.cs new file mode 100644 index 0000000..0729dc7 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/ISerializationProvider.cs @@ -0,0 +1,14 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Threading.Tasks; + +namespace STX.Serialization.Providers.Abstractions +{ + public interface ISerializationProvider + { + ValueTask Serialize(T obj); + ValueTask Deserialize(string json); + } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyException.cs new file mode 100644 index 0000000..20fafda --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyException.cs @@ -0,0 +1,9 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public interface ISerializationDependencyException + { } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyValidation.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyValidation.cs new file mode 100644 index 0000000..44e1739 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationDependencyValidation.cs @@ -0,0 +1,9 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public interface ISerializationDependencyValidationException + { } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationServiceException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationServiceException.cs new file mode 100644 index 0000000..8e8504e --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationServiceException.cs @@ -0,0 +1,9 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public interface ISerializationServiceException + { } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationValidationException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationValidationException.cs new file mode 100644 index 0000000..2cf2a32 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/ISerializationValidationException.cs @@ -0,0 +1,9 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public interface ISerializationValidationException + { } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationDependencyProviderException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationDependencyProviderException.cs new file mode 100644 index 0000000..97f5868 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationDependencyProviderException.cs @@ -0,0 +1,20 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Collections; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public class SerializationDependencyProviderException : Xeption + { + public SerializationDependencyProviderException(string message, Xeption innerException) + : base(message, innerException) + { } + + public SerializationDependencyProviderException(string message, Xeption innerException, IDictionary data) + : base(message, innerException, data) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationServiceProviderException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationServiceProviderException.cs new file mode 100644 index 0000000..1294440 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationServiceProviderException.cs @@ -0,0 +1,20 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Collections; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public class SerializationServiceProviderException : Xeption + { + public SerializationServiceProviderException(string message, Xeption innerException) + : base(message, innerException) + { } + + public SerializationServiceProviderException(string message, Xeption innerException, IDictionary data) + : base(message, innerException, data) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationValidationProviderException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationValidationProviderException.cs new file mode 100644 index 0000000..6b79c20 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/SerializationValidationProviderException.cs @@ -0,0 +1,20 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System.Collections; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + public class SerializationValidationProviderException : Xeption + { + public SerializationValidationProviderException(string message, Xeption innerException) + : base(message, innerException) + { } + + public SerializationValidationProviderException(string message, Xeption innerException, IDictionary data) + : base(message, innerException, data) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions/Models/Exceptions/UncatagorizedSerializationProviderException.cs b/STX.Serialization.Providers.Abstractions/Models/Exceptions/UncatagorizedSerializationProviderException.cs new file mode 100644 index 0000000..55815ee --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/Models/Exceptions/UncatagorizedSerializationProviderException.cs @@ -0,0 +1,21 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions.Models.Exceptions +{ + internal class UncatagorizedSerializationProviderException : Xeption + { + public UncatagorizedSerializationProviderException(string message, Exception innerException) + : base(message, innerException) + { } + + public UncatagorizedSerializationProviderException(string message, Exception innerException, IDictionary data) + : base(message, innerException, data) + { } + } +} diff --git a/STX.Serialization.Providers.Abstractions/STX.Serialization.Providers.Abstractions.csproj b/STX.Serialization.Providers.Abstractions/STX.Serialization.Providers.Abstractions.csproj index 41ce98a..c7af126 100644 --- a/STX.Serialization.Providers.Abstractions/STX.Serialization.Providers.Abstractions.csproj +++ b/STX.Serialization.Providers.Abstractions/STX.Serialization.Providers.Abstractions.csproj @@ -28,6 +28,16 @@ True + + + + + + + + + + true diff --git a/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.Exceptions.cs b/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.Exceptions.cs new file mode 100644 index 0000000..30fb743 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.Exceptions.cs @@ -0,0 +1,95 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Threading.Tasks; +using STX.Serialization.Providers.Abstractions.Models.Exceptions; +using Xeptions; + +namespace STX.Serialization.Providers.Abstractions +{ + public partial class SerializationAbstractionProvider : ISerializationAbstractionProvider + { + private async ValueTask TryCatch( + Func> asyncFunction) + { + try + { + return await asyncFunction(); + } + catch (Exception exception) when (exception is ISerializationValidationException) + { + throw CreateValidationException(exception); + } + catch (Exception exception) when (exception is ISerializationDependencyValidationException) + { + throw CreateValidationException(exception); + } + catch (Exception exception) when (exception is ISerializationDependencyException) + { + throw CreateDependencyException(exception); + } + catch (Exception exception) when (exception is ISerializationServiceException) + { + throw CreateServiceException(exception); + } + catch (Exception exception) + { + var uncatagorizedSerializationProviderException = + new UncatagorizedSerializationProviderException( + message: "Serialization provider not properly implemented. Uncatagorized errors found, " + + "contact the serialization provider owner for support.", + innerException: exception, + data: exception.Data); + + throw CreateUncatagorizedServiceException(uncatagorizedSerializationProviderException); + } + } + + private SerializationValidationProviderException CreateValidationException( + Exception exception) + { + var serializationValidationException = + new SerializationValidationProviderException( + message: "Serialization validation errors occurred, please try again.", + innerException: exception as Xeption, + data: exception.Data); + + return serializationValidationException; + } + + private SerializationDependencyProviderException CreateDependencyException( + Exception exception) + { + var serializationDependencyException = new SerializationDependencyProviderException( + message: "Serialization dependency error occurred, contact support.", + innerException: exception as Xeption, + data: exception.Data); + + return serializationDependencyException; + } + + private SerializationServiceProviderException CreateServiceException( + Exception exception) + { + var serializationServiceException = new SerializationServiceProviderException( + message: "Serialization service error occurred, contact support.", + innerException: exception as Xeption, + data: exception.Data); + + return serializationServiceException; + } + + private SerializationServiceProviderException CreateUncatagorizedServiceException( + Exception exception) + { + var serializationServiceException = new SerializationServiceProviderException( + message: "Uncatagorized serialization service error occurred, contact support.", + innerException: exception as Xeption, + data: exception.Data); + + return serializationServiceException; + } + } +} diff --git a/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.cs b/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.cs new file mode 100644 index 0000000..4df0e91 --- /dev/null +++ b/STX.Serialization.Providers.Abstractions/SerializationAbstractionProvider.cs @@ -0,0 +1,26 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Threading.Tasks; + +namespace STX.Serialization.Providers.Abstractions +{ + public partial class SerializationAbstractionProvider : ISerializationAbstractionProvider + { + private readonly ISerializationProvider SerializationProvider; + + public SerializationAbstractionProvider(ISerializationProvider serializationProvider) => + SerializationProvider = serializationProvider; + + public ValueTask Serialize(T obj) => + TryCatch(() => + { + return this.SerializationProvider.Serialize(obj); + }); + + public ValueTask Deserialize(string json) => + throw new NotImplementedException(); + } +}