Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom exception for the HTTP 451 response #1239

Merged
merged 5 commits into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -559,7 +559,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
6 changes: 2 additions & 4 deletions SolutionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
[assembly: AssemblyVersionAttribute("0.19.0")]
[assembly: AssemblyFileVersionAttribute("0.19.0")]
[assembly: ComVisibleAttribute(false)]
namespace System
{
internal static class AssemblyVersionInformation
{
namespace System {
internal static class AssemblyVersionInformation {
internal const string Version = "0.19.0";
}
}