Merge pull request #73 from octokit/haacked/fix-validation-exception

Make sure ApiError is never null
This commit is contained in:
Phil Haack
2013-10-10 22:27:22 -07:00
2 changed files with 25 additions and 4 deletions
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using NSubstitute;
@@ -39,6 +40,22 @@ namespace Octokit.Tests.Exceptions
Assert.Equal(responseContent, exception.ApiValidationError.Message);
}
[Fact]
public void CreatesEmptyGitHubErrorWhenResponseBodyIsNull()
{
var response = Substitute.For<IResponse>();
response.Body.Returns("test");
var exception = new ApiValidationException();
var anotherException = new ApiValidationException("message1");
var thirdException = new ApiValidationException("message2", new InvalidOperationException());
// It's fine if the message is null when there's no response body as long as this doesn't throw.
Assert.Null(exception.ApiValidationError.Message);
Assert.Equal("message1", anotherException.ApiValidationError.Message);
Assert.Equal("message2", thirdException.ApiValidationError.Message);
}
#if !NETFX_CORE
[Fact]
public void CanPopulateObjectFromSerializedData()
+7 -3
View File
@@ -17,16 +17,17 @@ namespace Octokit
static readonly IJsonSerializer _jsonSerializer = new SimpleJsonSerializer();
public ApiValidationException()
: this(new ApiError(), null)
{
}
public ApiValidationException(string message)
: base(message)
: this(new ApiError { Message = message }, null)
{
}
public ApiValidationException(string message, Exception innerException)
: base(message, innerException)
: this(new ApiError { Message = message }, innerException)
{
}
@@ -75,7 +76,10 @@ namespace Octokit
try
{
if (responseContent != null)
return _jsonSerializer.Deserialize<ApiError>(responseContent) ?? new ApiError { Message = responseContent };
{
return _jsonSerializer.Deserialize<ApiError>(responseContent)
?? new ApiError { Message = responseContent };
}
}
catch (Exception)
{