Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Exmo.Tests/Exmo.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="1.3.0" PrivateAssets="all" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" />
<PackageReference Include="coverlet.msbuild" Version="6.0.4" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 5 additions & 7 deletions Exmo.Tests/ExmoApiExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<ExmoApiException>(obj);
var obj = JsonSerializer.Deserialize<ExceptionDto>(memoryStream);

var actualException = (ExmoApiException)obj;
var actualException = obj.ToExmoApiException();

Assert.IsType<ExmoApiException>(actualException);
Assert.Equal(message, actualException.Message);
Assert.Equal(code, actualException.Code);
}
Expand Down
30 changes: 30 additions & 0 deletions Exmo/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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 };
}
}
15 changes: 7 additions & 8 deletions Exmo/Exmo.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.1" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.9.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 0 additions & 14 deletions Exmo/ExmoApiException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Runtime.Serialization;

namespace Exmo
{
[Serializable]
public class ExmoApiException : Exception
{
public int? Code { get; set; }
Expand All @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,8 +12,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down