From cdd46465dbe8002377bc710cb8356d16197a519e Mon Sep 17 00:00:00 2001 From: Devesh Khandelwal Date: Sun, 10 Apr 2016 23:37:18 +0530 Subject: [PATCH] Add custom exception for the HTTP 451 response (#1239) * Add HTTP 451: Legal Takedown Exception. * Add LegalRestrictionException in HandleErrors. * Cast 451 to HttpStatusCode and include exception in csproj files. * Tests added and "FixProjects". * Fix: 403 -> 451 in 451Tests. --- .../LegalRestrictionExceptionTests.cs | 19 +++++ Octokit.Tests/Octokit.Tests.csproj | 1 + .../Exceptions/LegalRestrictionException.cs | 73 +++++++++++++++++++ Octokit/Http/Connection.cs | 3 +- Octokit/Octokit-Mono.csproj | 1 + Octokit/Octokit-MonoAndroid.csproj | 3 +- Octokit/Octokit-Monotouch.csproj | 3 +- Octokit/Octokit-Portable.csproj | 1 + Octokit/Octokit-netcore45.csproj | 1 + Octokit/Octokit.csproj | 1 + 10 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs create mode 100644 Octokit/Exceptions/LegalRestrictionException.cs diff --git a/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs b/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs new file mode 100644 index 0000000000..c6190cea50 --- /dev/null +++ b/Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Net; +using Octokit.Internal; +using Xunit; + +namespace Octokit.Tests.Exceptions +{ + public class LegalRestrictionExceptionTests + { + [Fact] + public void HasDefaultMessage() + { + var response = new Response((HttpStatusCode)451, null, new Dictionary(), "application/json"); + var legalRestrictionException = new LegalRestrictionException(response); + + Assert.Equal("Resource taken down due to a DMCA notice.", legalRestrictionException.Message); + } + } +} \ No newline at end of file diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 8aee294e26..1ba8aca18c 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -136,6 +136,7 @@ + diff --git a/Octokit/Exceptions/LegalRestrictionException.cs b/Octokit/Exceptions/LegalRestrictionException.cs new file mode 100644 index 0000000000..df115d4d64 --- /dev/null +++ b/Octokit/Exceptions/LegalRestrictionException.cs @@ -0,0 +1,73 @@ +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Net; +using System.Runtime.Serialization; + +namespace Octokit +{ + /// + /// Represents a HTTP 451 - Unavailable For Legal Reasons response returned from the API. + /// This will returned if GitHub has been asked to takedown the requested resource due to + /// a DMCA takedown. + /// +#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 LegalRestrictionException : ApiException + { + public override string Message + { + get { return ApiErrorMessageSafe ?? "Resource taken down due to a DMCA notice."; } + } + + /// + /// Constructs an instance of LegalRestrictionException + /// + /// The HTTP payload from the server + public LegalRestrictionException(IResponse response) : this(response, null) + { + } + + /// + /// Constructs an instance of LegalRestrictionException + /// + /// The exception message + /// The http status code returned by the response + public LegalRestrictionException(string message, HttpStatusCode statusCode) : base(message, statusCode) + { + } + + /// + /// Constructs an instance of LegalRestrictionException + /// + /// The HTTP payload from the server + /// The inner exception + public LegalRestrictionException(IResponse response, Exception innerException) + : base(response, innerException) + { + Debug.Assert(response != null && response.StatusCode == (HttpStatusCode)451, + "LegalRestrictionException created with wrong status code"); + } + +#if !NETFX_CORE + /// + /// Constructs an instance of LegalRestrictionException + /// + /// + /// The that holds the + /// serialized object data about the exception being thrown. + /// + /// + /// The that contains + /// contextual information about the source or destination. + /// + protected LegalRestrictionException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } +#endif + } +} diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index ac997f7c6a..ed5dd9576a 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -568,7 +568,8 @@ async Task RunRequest(IRequest request, CancellationToken cancellatio { HttpStatusCode.Unauthorized, GetExceptionForUnauthorized }, { HttpStatusCode.Forbidden, GetExceptionForForbidden }, { HttpStatusCode.NotFound, response => new NotFoundException(response) }, - { (HttpStatusCode)422, response => new ApiValidationException(response) } + { (HttpStatusCode)422, response => new ApiValidationException(response) }, + { (HttpStatusCode)451, response => new LegalRestrictionException(response) } }; static void HandleErrors(IResponse response) diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index abbfe34868..4b28ca0679 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -458,6 +458,7 @@ + \ No newline at end of file diff --git a/Octokit/Octokit-MonoAndroid.csproj b/Octokit/Octokit-MonoAndroid.csproj index 60896d144f..4396b3c899 100644 --- a/Octokit/Octokit-MonoAndroid.csproj +++ b/Octokit/Octokit-MonoAndroid.csproj @@ -467,6 +467,7 @@ + - + \ No newline at end of file diff --git a/Octokit/Octokit-Monotouch.csproj b/Octokit/Octokit-Monotouch.csproj index 41d2a7e080..10562bbe35 100644 --- a/Octokit/Octokit-Monotouch.csproj +++ b/Octokit/Octokit-Monotouch.csproj @@ -463,7 +463,8 @@ + - + \ No newline at end of file diff --git a/Octokit/Octokit-Portable.csproj b/Octokit/Octokit-Portable.csproj index 0f95001916..17259fe6bb 100644 --- a/Octokit/Octokit-Portable.csproj +++ b/Octokit/Octokit-Portable.csproj @@ -455,6 +455,7 @@ + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index 3cfdc341b9..73e365166b 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -462,6 +462,7 @@ + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index f6004fb9c3..f4944f25b4 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -91,6 +91,7 @@ +