diff --git a/Exmo.Tests/Exmo.Tests.csproj b/Exmo.Tests/Exmo.Tests.csproj index 7c0e0b1..8f97497 100644 --- a/Exmo.Tests/Exmo.Tests.csproj +++ b/Exmo.Tests/Exmo.Tests.csproj @@ -1,16 +1,16 @@  - netcoreapp3.1 + net10.0 false - - - - - + + + + + diff --git a/Exmo.Tests/ExmoApiExceptionTests.cs b/Exmo.Tests/ExmoApiExceptionTests.cs index 91c9679..9a5a78c 100644 --- a/Exmo.Tests/ExmoApiExceptionTests.cs +++ b/Exmo.Tests/ExmoApiExceptionTests.cs @@ -1,13 +1,11 @@ using System.IO; -using System.Runtime.Serialization.Formatters.Binary; +using System.Text.Json; using Xunit; namespace Exmo.Tests { public class ExmoApiExceptionTests { - private readonly BinaryFormatter _binaryFormatter = new BinaryFormatter(); - [Theory] [InlineData("Test Error", 1234)] [InlineData("Test Error", null)] @@ -16,13 +14,13 @@ public void ExmoApiException_Deserialize(string message, int? code) var originalException = new ExmoApiException(message) { Code = code }; using var memoryStream = new MemoryStream(); - _binaryFormatter.Serialize(memoryStream, originalException); + JsonSerializer.Serialize(memoryStream, originalException.ToDto()); memoryStream.Position = 0; - var obj = _binaryFormatter.Deserialize(memoryStream); - Assert.IsType(obj); + var obj = JsonSerializer.Deserialize(memoryStream); - var actualException = (ExmoApiException)obj; + var actualException = obj.ToExmoApiException(); + Assert.IsType(actualException); Assert.Equal(message, actualException.Message); Assert.Equal(code, actualException.Code); } diff --git a/Exmo/ExceptionExtensions.cs b/Exmo/ExceptionExtensions.cs new file mode 100644 index 0000000..591e914 --- /dev/null +++ b/Exmo/ExceptionExtensions.cs @@ -0,0 +1,30 @@ +using System; +#nullable enable + +namespace Exmo +{ + public sealed record ExceptionDto( + string Type, + string Message, + string? StackTrace, + int HResult, + ExceptionDto? Inner, + int? Code + ); + + public static class ExceptionExtensions + { + public static ExceptionDto ToDto(this Exception ex) + => new ExceptionDto( + ex.GetType().FullName ?? ex.GetType().Name, + ex.Message, + ex.StackTrace, + ex.HResult, + ex.InnerException?.ToDto(), + ex is ExmoApiException ea ? ea.Code : null + ); + + public static ExmoApiException ToExmoApiException(this ExceptionDto dto) + => new ExmoApiException(dto.Message) { Code = dto.Code }; + } +} diff --git a/Exmo/Exmo.csproj b/Exmo/Exmo.csproj index 23d6cf1..430a2b9 100644 --- a/Exmo/Exmo.csproj +++ b/Exmo/Exmo.csproj @@ -1,17 +1,16 @@ - netstandard2.0 + net10.0 - - - - - - - + + + + + + diff --git a/Exmo/ExmoApiException.cs b/Exmo/ExmoApiException.cs index df1914f..74f788a 100644 --- a/Exmo/ExmoApiException.cs +++ b/Exmo/ExmoApiException.cs @@ -1,9 +1,7 @@ using System; -using System.Runtime.Serialization; namespace Exmo { - [Serializable] public class ExmoApiException : Exception { public int? Code { get; set; } @@ -21,17 +19,5 @@ public ExmoApiException(string message, Exception innerException) : base(message, innerException) { } - - protected ExmoApiException(SerializationInfo serializationInfo, StreamingContext streamingContext) - : base(serializationInfo, streamingContext) - { - Code = (int?)serializationInfo.GetValue(nameof(Code), typeof(int?)); - } - - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - base.GetObjectData(info, context); - info.AddValue(nameof(Code), Code); - } } } diff --git a/Sample/Sample.csproj b/Sample/Sample.csproj index 87657e3..8e1182b 100644 --- a/Sample/Sample.csproj +++ b/Sample/Sample.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net10.0 @@ -12,8 +12,8 @@ - - + +