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); + } } /// 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 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) { } + } +} 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); + } } }