-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
10 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Octokit.Tests/Exceptions/LegalRestrictionExceptionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters