Add default messages for custom exceptions

Right now, our exception messages come from the API response in most
cases. But if for any reason there isn't one, we supply a default. I
realized these messages might make it to people's logs so it might be
nice to spend time later making them even more useful.
This commit is contained in:
Haacked
2013-10-18 11:08:56 -07:00
parent a1887837be
commit c87944c884
15 changed files with 137 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ namespace Octokit.Tests.Exceptions
[InlineData("")]
[InlineData(null)]
[InlineData("{{{{{")]
[InlineData("<html><body><h1>502 Bad Gateway</h1>The server returned an invalid or incomplete response.</body></html>")]
public void CreatesGitHubErrorIfResponseMessageIsNotValidJson(string responseContent)
{
var response = new ApiResponse<object>

View File

@@ -27,6 +27,19 @@ namespace Octokit.Tests.Exceptions
Assert.Equal("key is already in use", exception.ApiError.Errors.First().Message);
}
[Fact]
public void ProvidesDefaultMessage()
{
var response = new ApiResponse<object>
{
StatusCode = (HttpStatusCode)422
};
var exception = new ApiValidationException(response);
Assert.Equal("Validation Failed", exception.Message);
}
#if !NETFX_CORE
[Fact]
public void CanPopulateObjectFromSerializedData()

View File

@@ -18,6 +18,15 @@ namespace Octokit.Tests.Exceptions
Assert.Equal("YOU SHALL NOT PASS!", forbiddenException.ApiError.Message);
}
[Fact]
public void HasDefaultMessage()
{
var response = new ApiResponse<object> { StatusCode = HttpStatusCode.Forbidden };
var forbiddenException = new ForbiddenException(response);
Assert.Equal("Request Forbidden", forbiddenException.Message);
}
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Octokit.Internal;
using Xunit;
namespace Octokit.Tests.Exceptions
{
public class LoginAttemptsExceededExceptionTests
{
public class TheConstructor
{
[Fact]
public void SetsDefaultMessage()
{
var response = new ApiResponse<object>
{
StatusCode = HttpStatusCode.Forbidden
};
var exception = new LoginAttemptsExceededException(response);
Assert.Equal("Maximum number of login attempts exceeded", exception.Message);
}
}
}
}

View File

@@ -28,6 +28,8 @@ namespace Octokit.Tests.Exceptions
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal("API Rate Limit exceeded", exception.Message);
Assert.Equal(expectedReset, exception.Reset);
}

View File

@@ -0,0 +1,18 @@
using Xunit;
namespace Octokit.Tests.Exceptions
{
public class TwoFactorChallengeFailedExceptionTests
{
public class TheConstructor
{
[Fact]
public void SetsDefaultMessage()
{
var exception = new TwoFactorChallengeFailedException();
Assert.Equal("The two-factor authentication code supplied is not correct", exception.Message);
}
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Octokit.Internal;
using Xunit;
namespace Octokit.Tests.Exceptions
{
public class TwoFactorRequiredExceptionTests
{
public class TheConstructor
{
[Fact]
public void SetsDefaultMessage()
{
var response = new ApiResponse<object>
{
StatusCode = HttpStatusCode.Unauthorized
};
var exception = new TwoFactorRequiredException(response, TwoFactorType.Sms);
Assert.Equal("Two-factor authentication code is required", exception.Message);
}
}
}
}

View File

@@ -64,8 +64,11 @@
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\ForbiddenExceptionTests.cs" />
<Compile Include="Exceptions\LoginAttemptsExceededExceptionTests.cs" />
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />

View File

@@ -57,7 +57,10 @@
<Compile Include="Exceptions\ApiExceptionTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Exceptions\ForbiddenExceptionTests.cs" />
<Compile Include="Exceptions\LoginAttemptsExceededExceptionTests.cs" />
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />

View File

@@ -36,7 +36,7 @@ namespace Octokit
public override string Message
{
get { return ApiErrorMessageSafe; }
get { return ApiErrorMessageSafe ?? "Request Forbidden"; }
}
public HttpStatusCode StatusCode { get; private set; }

View File

@@ -33,6 +33,11 @@ namespace Octokit
"ApiValidationException created with wrong status code");
}
public override string Message
{
get { return ApiErrorMessageSafe ?? "Validation Failed"; }
}
#if !NETFX_CORE
protected ApiValidationException(SerializationInfo info, StreamingContext context)
: base(info, context)

View File

@@ -20,6 +20,11 @@ namespace Octokit
{
}
public override string Message
{
get { return ApiErrorMessageSafe ?? "Maximum number of login attempts exceeded"; }
}
#if !NETFX_CORE
protected LoginAttemptsExceededException(SerializationInfo info, StreamingContext context)
: base(info, context)

View File

@@ -52,6 +52,13 @@ namespace Octokit
/// </summary>
public DateTimeOffset Reset { get; private set; }
// TODO: Might be nice to have this provide a more detailed message such as what the limit is,
// how many are remaining, and when it will reset. I'm too lazy to do it now.
public override string Message
{
get { return ApiErrorMessageSafe ?? "API Rate Limit exceeded"; }
}
#if !NETFX_CORE
protected RateLimitExceededException(SerializationInfo info, StreamingContext context)
: base(info, context)

View File

@@ -23,6 +23,11 @@ namespace Octokit
{
}
public override string Message
{
get { return "The two-factor authentication code supplied is not correct"; }
}
#if !NETFX_CORE
protected TwoFactorChallengeFailedException(SerializationInfo info, StreamingContext context)
: base(info, context)

View File

@@ -31,6 +31,11 @@ namespace Octokit
TwoFactorType = twoFactorType;
}
public override string Message
{
get { return ApiErrorMessageSafe ?? "Two-factor authentication code is required"; }
}
#if !NETFX_CORE
protected TwoFactorRequiredException(SerializationInfo info, StreamingContext context)
: base(info, context)