diff --git a/Octokit.Tests/Exceptions/ApiExceptionTests.cs b/Octokit.Tests/Exceptions/ApiExceptionTests.cs index ec4afe61..4115bfce 100644 --- a/Octokit.Tests/Exceptions/ApiExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiExceptionTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Formatters.Binary; @@ -13,6 +14,33 @@ namespace Octokit.Tests.Exceptions { public class TheConstructor { + [Fact] + public void SetsDefaultExceptionMessage() + { + var exception = new ApiException(); + Assert.Equal("An error occurred with this API request", exception.Message); + } + + [Fact] + public void SetsSpecifiedExceptionMessageAndInnerException() + { + var inner = new InvalidOperationException(); + + var exception = new ApiException("Shit broke", inner); + + Assert.Equal("Shit broke", exception.Message); + Assert.Same(inner, exception.InnerException); + } + + [Fact] + public void SetsSpecifiedExceptionMessageAndStatusCode() + { + var exception = new ApiException("Shit still broke", HttpStatusCode.Gone); + + Assert.Equal("Shit still broke", exception.Message); + Assert.Equal(HttpStatusCode.Gone, exception.StatusCode); + } + [Fact] public void CreatesGitHubErrorFromJsonResponse() { diff --git a/Octokit/Exceptions/ApiException.cs b/Octokit/Exceptions/ApiException.cs index 2aa9fe72..85a1db47 100644 --- a/Octokit/Exceptions/ApiException.cs +++ b/Octokit/Exceptions/ApiException.cs @@ -20,6 +20,16 @@ namespace Octokit { } + public ApiException(string message, HttpStatusCode httpStatusCode) + : this(new ApiResponse {Body = message, StatusCode = httpStatusCode}) + { + } + + public ApiException(string message, Exception innerException) + : this(new ApiResponse { Body = message }, innerException) + { + } + public ApiException(IResponse response) : this(response, null) { @@ -36,7 +46,7 @@ namespace Octokit public override string Message { - get { return ApiErrorMessageSafe ?? "Request Forbidden"; } + get { return ApiErrorMessageSafe ?? "An error occurred with this API request"; } } public HttpStatusCode StatusCode { get; private set; } diff --git a/Octokit/Exceptions/ForbiddenException.cs b/Octokit/Exceptions/ForbiddenException.cs index c99af59b..efc16737 100644 --- a/Octokit/Exceptions/ForbiddenException.cs +++ b/Octokit/Exceptions/ForbiddenException.cs @@ -24,6 +24,10 @@ namespace Octokit "ForbiddenException created with wrong status code"); } + public override string Message + { + get { return ApiErrorMessageSafe ?? "Request Forbidden"; } + } #if !NETFX_CORE protected ForbiddenException(SerializationInfo info, StreamingContext context)