mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-07 12:26:18 +00:00
Make ApiError is never null
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user