From 70b94187b3e65479f0197994ac66b752ea5f03e2 Mon Sep 17 00:00:00 2001 From: Haacked Date: Tue, 22 Oct 2013 17:12:53 -0700 Subject: [PATCH] Add NotFoundException for 404 status --- Octokit.Tests/Http/ConnectionTests.cs | 22 +++++++++++++++++ Octokit/Exceptions/NotFoundException.cs | 33 +++++++++++++++++++++++++ Octokit/Http/Connection.cs | 5 ++++ Octokit/Octokit.csproj | 1 + Octokit/OctokitRT.csproj | 1 + 5 files changed, 62 insertions(+) create mode 100644 Octokit/Exceptions/NotFoundException.cs diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 263d048f..00f484d3 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -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(); + IResponse response = new ApiResponse + { + StatusCode = HttpStatusCode.NotFound, + Body = "GONE BYE BYE!" + }; + httpClient.Send(Args.Request).Returns(Task.FromResult(response)); + var connection = new Connection("Test Runner User Agent", + ExampleUri, + Substitute.For(), + httpClient, + Substitute.For()); + + var exception = await AssertEx.Throws( + async () => await connection.GetAsync(new Uri("/endpoint", UriKind.Relative))); + + Assert.Equal("GONE BYE BYE!", exception.Message); + } + [Fact] public async Task ThrowsForbiddenExceptionForUnknownForbiddenResponse() { diff --git a/Octokit/Exceptions/NotFoundException.cs b/Octokit/Exceptions/NotFoundException.cs new file mode 100644 index 00000000..72995802 --- /dev/null +++ b/Octokit/Exceptions/NotFoundException.cs @@ -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 + } +} diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index 3bb0fc64..c301f3d3 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -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); diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index 76ef5bfd..ab75a36a 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -82,6 +82,7 @@ Properties\SolutionInfo.cs + diff --git a/Octokit/OctokitRT.csproj b/Octokit/OctokitRT.csproj index 808418ef..1c4806e2 100644 --- a/Octokit/OctokitRT.csproj +++ b/Octokit/OctokitRT.csproj @@ -122,6 +122,7 @@ +