Merge pull request #974 from asizikov/api-exception-prints-payload-on-tostring

ToString for ApiException should contain response payload
This commit is contained in:
Brendan Forster
2015-12-10 22:41:04 +10:30
3 changed files with 96 additions and 4 deletions
+4 -4
View File
@@ -10,10 +10,7 @@ namespace Octokit.Tests.Exceptions
{
public class ApiErrorTests
{
[Fact]
public void CanBeDeserialized()
{
const string json = @"{
const string json = @"{
""message"": ""Validation Failed"",
""errors"": [
{
@@ -23,6 +20,9 @@ namespace Octokit.Tests.Exceptions
}
]
}";
[Fact]
public void CanBeDeserialized()
{
var serializer = new SimpleJsonSerializer();
var apiError = serializer.Deserialize<ApiError>(json);
@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NSubstitute;
using Octokit.Internal;
using Xunit;
@@ -120,5 +121,72 @@ namespace Octokit.Tests.Exceptions
}
#endif
}
public class TheToStringMethod
{
[Fact]
public void ContainsResponseBody()
{
const string responseBody = @"{""errors"":[{""code"":""custom"",""field"":""key"",""message"":""key is " +
@"already in use"",""resource"":""PublicKey""}],""message"":""Validation Failed""}";
var response = new Response(
HttpStatusCode.GatewayTimeout,
responseBody,
new Dictionary<string, string>(),
"application/json"
);
var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
Assert.Contains(responseBody, stringRepresentation);
}
[Fact]
public void DoesNotThrowIfBodyIsNotDefined()
{
var response = new Response(
HttpStatusCode.GatewayTimeout,
null,
new Dictionary<string, string>(),
"application/json"
);
var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
Assert.NotNull(stringRepresentation);
}
[Fact]
public void DoesNotPrintImageContent()
{
var responceBody = new byte[0];
var response = new Response(
HttpStatusCode.GatewayTimeout,
responceBody,
new Dictionary<string, string>(),
"image/*"
);
var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
Assert.NotNull(stringRepresentation);
}
[Fact]
public void DoesNotPrintNonStringContent()
{
var responceBody = new byte[0];
var response = new Response(
HttpStatusCode.GatewayTimeout,
responceBody,
new Dictionary<string, string>(),
"application/json"
);
var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
Assert.NotNull(stringRepresentation);
}
}
}
}
+24
View File
@@ -179,5 +179,29 @@ namespace Octokit
return ApiError != null ? ApiError.Message : null;
}
}
/// <summary>
/// Get the inner http response body from the API response
/// </summary>
/// <remarks>
/// Returns empty string if HttpResponse is not populated or if
/// response body is not a string
/// </remarks>
protected string HttpResponseBodySafe
{
get
{
return HttpResponse != null
&& !HttpResponse.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)
&& HttpResponse.Body is string
? (string)HttpResponse.Body : string.Empty;
}
}
public override string ToString()
{
var original = base.ToString();
return original + Environment.NewLine + HttpResponseBodySafe ;
}
}
}