Skip to content

Commit

Permalink
Add custom exception for the HTTP 451 response (#1239)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
devkhan authored and shiftkey committed Apr 10, 2016
1 parent 49f246f commit cdd4646
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>(), "application/json");
var legalRestrictionException = new LegalRestrictionException(response);

Assert.Equal("Resource taken down due to a DMCA notice.", legalRestrictionException.Message);
}
}
}
1 change: 1 addition & 0 deletions Octokit.Tests/Octokit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Exceptions\ApiErrorTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Exceptions\LegalRestrictionExceptionTests.cs" />
<Compile Include="Exceptions\RepositoryExistsExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\ForbiddenExceptionTests.cs" />
Expand Down
73 changes: 73 additions & 0 deletions Octokit/Exceptions/LegalRestrictionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.Serialization;

namespace Octokit
{
/// <summary>
/// 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.
/// </summary>
#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."; }
}

/// <summary>
/// Constructs an instance of LegalRestrictionException
/// </summary>
/// <param name="response">The HTTP payload from the server</param>
public LegalRestrictionException(IResponse response) : this(response, null)
{
}

/// <summary>
/// Constructs an instance of LegalRestrictionException
/// </summary>
/// <param name="message">The exception message</param>
/// <param name="statusCode">The http status code returned by the response</param>
public LegalRestrictionException(string message, HttpStatusCode statusCode) : base(message, statusCode)
{
}

/// <summary>
/// Constructs an instance of LegalRestrictionException
/// </summary>
/// <param name="response">The HTTP payload from the server</param>
/// <param name="innerException">The inner exception</param>
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
/// <summary>
/// Constructs an instance of LegalRestrictionException
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo"/> that holds the
/// serialized object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="StreamingContext"/> that contains
/// contextual information about the source or destination.
/// </param>
protected LegalRestrictionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}
3 changes: 2 additions & 1 deletion Octokit/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ async Task<IResponse> 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)
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@
<Compile Include="Models\Request\UserRename.cs" />
<Compile Include="Models\Response\UserRenameResponse.cs" />
<Compile Include="Models\Request\NewPublicKey.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
3 changes: 2 additions & 1 deletion Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
<Compile Include="Models\Request\UserRename.cs" />
<Compile Include="Models\Response\UserRenameResponse.cs" />
<Compile Include="Models\Request\NewPublicKey.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
</Project>
3 changes: 2 additions & 1 deletion Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@
<Compile Include="Models\Request\UserRename.cs" />
<Compile Include="Models\Response\UserRenameResponse.cs" />
<Compile Include="Models\Request\NewPublicKey.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
<Compile Include="Models\Request\UserRename.cs" />
<Compile Include="Models\Response\UserRenameResponse.cs" />
<Compile Include="Models\Request\NewPublicKey.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@
<Compile Include="Models\Request\UserRename.cs" />
<Compile Include="Models\Response\UserRenameResponse.cs" />
<Compile Include="Models\Request\NewPublicKey.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Clients\UserKeysClient.cs" />
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Exceptions\InvalidGitIgnoreTemplateException.cs" />
<Compile Include="Exceptions\LegalRestrictionException.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
Expand Down

0 comments on commit cdd4646

Please sign in to comment.