Add NotFoundException for 404 status

This commit is contained in:
Haacked
2013-10-22 17:12:53 -07:00
parent ad210cecc7
commit 70b94187b3
5 changed files with 62 additions and 0 deletions
+22
View File
@@ -231,6 +231,28 @@ namespace Octokit.Tests.Http
Assert.Equal("http://developer.github.com/v3", exception.ApiError.DocumentationUrl);
}
[Fact]
public async Task ThrowsNotFoundExceptionForFileNotFoundResponse()
{
var httpClient = Substitute.For<IHttpClient>();
IResponse<string> response = new ApiResponse<string>
{
StatusCode = HttpStatusCode.NotFound,
Body = "GONE BYE BYE!"
};
httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response));
var connection = new Connection("Test Runner User Agent",
ExampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());
var exception = await AssertEx.Throws<NotFoundException>(
async () => await connection.GetAsync<string>(new Uri("/endpoint", UriKind.Relative)));
Assert.Equal("GONE BYE BYE!", exception.Message);
}
[Fact]
public async Task ThrowsForbiddenExceptionForUnknownForbiddenResponse()
{
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.Serialization;
namespace Octokit
{
#if !NETFX_CORE
[Serializable]
#endif
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors",
Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")]
public class NotFoundException : ApiException
{
public NotFoundException(IResponse response) : this(response, null)
{
}
public NotFoundException(IResponse response, Exception innerException) : base(response, innerException)
{
Debug.Assert(response != null && response.StatusCode == HttpStatusCode.NotFound,
"NotFoundException created with wrong status code");
}
#if !NETFX_CORE
protected NotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}
+5
View File
@@ -244,6 +244,11 @@ namespace Octokit
throw exceptionFunc(response);
}
if ((int)response.StatusCode == 404)
{
throw new NotFoundException(response);
}
if ((int)response.StatusCode >= 400)
{
throw new ApiException(response);
+1
View File
@@ -82,6 +82,7 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\IssuesClient.cs" />
<Compile Include="Exceptions\NotFoundException.cs" />
<Compile Include="IIssuesClient.cs" />
<Compile Include="Models\Issue.cs" />
<Compile Include="Models\IssueRequest.cs" />
+1
View File
@@ -122,6 +122,7 @@
<Compile Include="Exceptions\ApiException.cs" />
<Compile Include="Exceptions\ApiValidationException.cs" />
<Compile Include="Exceptions\AuthorizationException.cs" />
<Compile Include="Exceptions\NotFoundException.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\TwoFactorRequiredException.cs" />
<Compile Include="Helpers\ApiUrls.cs" />