From eb09bfc94e59f246875f50e69c0c894c37051129 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sat, 15 Sep 2018 17:19:59 -0400 Subject: [PATCH 1/5] Add UnrecognizedResponseException --- .../UnrecognizedResponseException.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Tgstation.Server.Client/UnrecognizedResponseException.cs diff --git a/src/Tgstation.Server.Client/UnrecognizedResponseException.cs b/src/Tgstation.Server.Client/UnrecognizedResponseException.cs new file mode 100644 index 0000000000..24dfa9227d --- /dev/null +++ b/src/Tgstation.Server.Client/UnrecognizedResponseException.cs @@ -0,0 +1,40 @@ +using System; +using System.Globalization; +using System.Net; +using Tgstation.Server.Api.Models; + +namespace Tgstation.Server.Client +{ + sealed class UnrecognizedResponseException : ClientException + { + /// + /// Construct an with the of a response body and the + /// + /// The body of the response + /// The for the + public UnrecognizedResponseException(string data, HttpStatusCode statusCode) : base(new ErrorMessage + { + Message = String.Format(CultureInfo.InvariantCulture, "Unrecognized response body: {0}", data), + SeverApiVersion = null + }, statusCode) + { } + + /// + /// Construct a + /// + public UnrecognizedResponseException() { } + + /// + /// Construct an with a + /// + /// The message for the + public UnrecognizedResponseException(string message) : base(message) { } + + /// + /// Construct an with a and + /// + /// The message for the + /// The inner for the base + public UnrecognizedResponseException(string message, Exception innerException) : base(message, innerException) { } + } +} From 06fb98aaca625bc3e75cb156ae5387021f0822d2 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sat, 15 Sep 2018 17:27:03 -0400 Subject: [PATCH 2/5] Throw UnrecognizedResponseExceptions from ApiClient --- src/Tgstation.Server.Client/ApiClient.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Client/ApiClient.cs b/src/Tgstation.Server.Client/ApiClient.cs index b7df25aa7c..66a2609031 100644 --- a/src/Tgstation.Server.Client/ApiClient.cs +++ b/src/Tgstation.Server.Client/ApiClient.cs @@ -164,7 +164,14 @@ namespace Tgstation.Server.Client if (String.IsNullOrWhiteSpace(json)) json = JsonConvert.SerializeObject(new object()); - return JsonConvert.DeserializeObject(json, serializerSettings); + try + { + return JsonConvert.DeserializeObject(json, serializerSettings); + } + catch (JsonException) + { + throw new UnrecognizedResponseException(json, response.StatusCode); + } } /// From 537805f86d18783e81c509acddf59e1a8abcb06e Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sat, 15 Sep 2018 17:30:10 -0400 Subject: [PATCH 3/5] Add TestUnrecognizedResponse Client test --- .../TestApiClient.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Tgstation.Server.Client.Tests/TestApiClient.cs b/tests/Tgstation.Server.Client.Tests/TestApiClient.cs index 4882fae2cc..2a544b5863 100644 --- a/tests/Tgstation.Server.Client.Tests/TestApiClient.cs +++ b/tests/Tgstation.Server.Client.Tests/TestApiClient.cs @@ -44,5 +44,28 @@ namespace Tgstation.Server.Client.Tests var result = await client.Read(Routes.Byond, default).ConfigureAwait(false); Assert.AreEqual(sample.Version, result.Version); } + + [TestMethod] + public async Task TestUnrecognizedResponse() + { + var sample = new Byond + { + Version = new Version(511, 1385) + }; + + var fakeJson = "asdfasd <>F#(*)U*#JLI"; + + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(fakeJson) + }; + + var httpClient = new Mock(); + httpClient.Setup(x => x.SendAsync(It.IsNotNull(), It.IsAny())).Returns(Task.FromResult(response)); + + var client = new ApiClient(httpClient.Object, new Uri("http://fake.com"), new ApiHeaders(new ProductHeaderValue("fake"), "fake")); + + await Assert.ThrowsExceptionAsync(() => client.Read(Routes.Byond, default)).ConfigureAwait(false); + } } } From 7d192b3a6e8992c82492e3bdf3e860888418c993 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sat, 15 Sep 2018 17:30:39 -0400 Subject: [PATCH 4/5] Bump client preview version --- src/Tgstation.Server.Client/Tgstation.Server.Client.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj b/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj index b3ad661fa0..34cf1a89d8 100644 --- a/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj +++ b/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj @@ -3,7 +3,7 @@ netstandard2.0 Full - 4.0.0.0-preview9106 + 4.0.0.0-preview9107 true Cyberboss /tg/station 13