Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #181 Remove exception serialization #182

Merged
merged 2 commits into from
Dec 23, 2023
Merged
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
2 changes: 1 addition & 1 deletion build/version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# Patch: Backwards compatible bug fixes only
# -Suffix (optional): a hyphen followed by a string denoting a pre-release version (rc1, rc2, etc.)

9.0.0-rc1
9.0.0-rc2
7 changes: 3 additions & 4 deletions src/ClashOfClans.Models/ClientError.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;

namespace ClashOfClans.Models
namespace ClashOfClans.Models
{
/// <summary>
/// Error message from the endpoint
/// </summary>
[Serializable]
public class ClientError
{
public string Reason { get; set; } = default!;

public string Message { get; set; } = default!;

public override string ToString() => $"Reason '{Reason}', message '{Message}'";
}
}
28 changes: 14 additions & 14 deletions src/ClashOfClans.Tests.Unit/ClashOfClansExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ClashOfClans.Core;
using ClashOfClans.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace ClashOfClans.Tests.Unit
{
Expand All @@ -22,25 +21,26 @@ public void ThrowClashOfClansException()
}

[TestMethod]
public void SerializeAndDeserializeClashOfClansExceptionUsingNewtonsoft()
public void ClashOfClansExceptionHasClientError()
{
// Arrange
var error = new ClientError
{
Message = "message",
Reason = "reason"
Reason = "Error reason",
Message = "Error message"
};
var originalException = new ClashOfClansException(error);

// Act
var output = JsonConvert.SerializeObject(originalException);
var deserializedException = JsonConvert.DeserializeObject<ClashOfClansException>(output);

// Assert
Assert.IsNotNull(deserializedException);
Assert.AreEqual(originalException.Message, deserializedException.Message);
Assert.AreEqual(originalException.Error.Message, deserializedException.Error.Message);
Assert.AreEqual(originalException.Error.Reason, deserializedException.Error.Reason);
try
{
// Act
throw new ClashOfClansException(error);
}
catch (ClashOfClansException ex)
{
// Assert
Assert.AreEqual(error.Reason, ex.Error.Reason);
Assert.AreEqual(error.Message, ex.Error.Message);
}
}
}
}
15 changes: 1 addition & 14 deletions src/ClashOfClans/Core/ClashOfClansException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using ClashOfClans.Models;
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace ClashOfClans.Core
{
/// <summary>
/// Represents errors that occur when communicating with the official Clash of Clans API
/// </summary>
[Serializable]
public class ClashOfClansException : Exception
{
/// <summary>
Expand Down Expand Up @@ -39,16 +36,6 @@ public ClashOfClansException(ClientError error)
Error = error;
}

protected ClashOfClansException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Error = (ClientError)info.GetValue(nameof(Error), typeof(ClientError))!;
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(Error), Error);
}
public override string Message => Error.ToString();
}
}