mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-30 16:39:21 +01:00
Merge branch 'master' of https://github.com/tgstation/tgstation-server into BetterDocker
This commit is contained in:
@@ -164,7 +164,14 @@ namespace Tgstation.Server.Client
|
||||
if (String.IsNullOrWhiteSpace(json))
|
||||
json = JsonConvert.SerializeObject(new object());
|
||||
|
||||
return JsonConvert.DeserializeObject<TResult>(json, serializerSettings);
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<TResult>(json, serializerSettings);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
throw new UnrecognizedResponseException(json, response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<DebugType>Full</DebugType>
|
||||
<Version>4.0.0.0-preview9106</Version>
|
||||
<Version>4.0.0.0-preview9107</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>Cyberboss</Authors>
|
||||
<Company>/tg/station 13</Company>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Construct an <see cref="UnrecognizedResponseException"/> with the <paramref name="data"/> of a response body and the <paramref name="statusCode"/>
|
||||
/// </summary>
|
||||
/// <param name="data">The body of the response</param>
|
||||
/// <param name="statusCode">The <see cref="HttpStatusCode"/> for the <see cref="ClientException"/></param>
|
||||
public UnrecognizedResponseException(string data, HttpStatusCode statusCode) : base(new ErrorMessage
|
||||
{
|
||||
Message = String.Format(CultureInfo.InvariantCulture, "Unrecognized response body: {0}", data),
|
||||
SeverApiVersion = null
|
||||
}, statusCode)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Construct a <see cref="UnrecognizedResponseException"/>
|
||||
/// </summary>
|
||||
public UnrecognizedResponseException() { }
|
||||
|
||||
/// <summary>
|
||||
/// Construct an <see cref="UnrecognizedResponseException"/> with a <paramref name="message"/>
|
||||
/// </summary>
|
||||
/// <param name="message">The message for the <see cref="Exception"/></param>
|
||||
public UnrecognizedResponseException(string message) : base(message) { }
|
||||
|
||||
/// <summary>
|
||||
/// Construct an <see cref="UnrecognizedResponseException"/> with a <paramref name="message"/> and <paramref name="innerException"/>
|
||||
/// </summary>
|
||||
/// <param name="message">The message for the <see cref="Exception"/></param>
|
||||
/// <param name="innerException">The inner <see cref="Exception"/> for the base <see cref="Exception"/></param>
|
||||
public UnrecognizedResponseException(string message, Exception innerException) : base(message, innerException) { }
|
||||
}
|
||||
}
|
||||
@@ -44,5 +44,28 @@ namespace Tgstation.Server.Client.Tests
|
||||
var result = await client.Read<Byond>(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<IHttpClient>();
|
||||
httpClient.Setup(x => x.SendAsync(It.IsNotNull<HttpRequestMessage>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
|
||||
|
||||
var client = new ApiClient(httpClient.Object, new Uri("http://fake.com"), new ApiHeaders(new ProductHeaderValue("fake"), "fake"));
|
||||
|
||||
await Assert.ThrowsExceptionAsync<UnrecognizedResponseException>(() => client.Read<Byond>(Routes.Byond, default)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user