From 0b14747f7b86e42d8c6212f4895b01c7b1c13512 Mon Sep 17 00:00:00 2001 From: Tomi Parviainen Date: Sat, 23 Dec 2023 19:51:03 +0200 Subject: [PATCH] feat: #181 Remove exception serialization --- src/ClashOfClans.Models/ClientError.cs | 7 ++--- .../ClashOfClansExceptionTests.cs | 28 +++++++++---------- .../Core/ClashOfClansException.cs | 15 +--------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/ClashOfClans.Models/ClientError.cs b/src/ClashOfClans.Models/ClientError.cs index 8b7fb3c5..23c5503b 100644 --- a/src/ClashOfClans.Models/ClientError.cs +++ b/src/ClashOfClans.Models/ClientError.cs @@ -1,15 +1,14 @@ -using System; - -namespace ClashOfClans.Models +namespace ClashOfClans.Models { /// /// Error message from the endpoint /// - [Serializable] public class ClientError { public string Reason { get; set; } = default!; public string Message { get; set; } = default!; + + public override string ToString() => $"Reason '{Reason}', message '{Message}'"; } } diff --git a/src/ClashOfClans.Tests.Unit/ClashOfClansExceptionTests.cs b/src/ClashOfClans.Tests.Unit/ClashOfClansExceptionTests.cs index 21f7249a..c370800a 100644 --- a/src/ClashOfClans.Tests.Unit/ClashOfClansExceptionTests.cs +++ b/src/ClashOfClans.Tests.Unit/ClashOfClansExceptionTests.cs @@ -1,7 +1,6 @@ using ClashOfClans.Core; using ClashOfClans.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json; namespace ClashOfClans.Tests.Unit { @@ -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(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); + } } } } diff --git a/src/ClashOfClans/Core/ClashOfClansException.cs b/src/ClashOfClans/Core/ClashOfClansException.cs index 42f714a2..ffdcb118 100644 --- a/src/ClashOfClans/Core/ClashOfClansException.cs +++ b/src/ClashOfClans/Core/ClashOfClansException.cs @@ -1,14 +1,11 @@ using ClashOfClans.Models; using System; -using System.Runtime.Serialization; -using System.Security.Permissions; namespace ClashOfClans.Core { /// /// Represents errors that occur when communicating with the official Clash of Clans API /// - [Serializable] public class ClashOfClansException : Exception { /// @@ -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(); } }