Skip to content

Commit

Permalink
feat: #181 Remove exception serialization (#182)
Browse files Browse the repository at this point in the history
* feat: #181 Remove exception serialization

* Update version.txt to 9.0.0-rc2
  • Loading branch information
tparviainen committed Dec 23, 2023
1 parent b4f4905 commit 1b83092
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
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();
}
}

0 comments on commit 1b83092

Please sign in to comment.