Add more info to TwoFactorChallengeFailedException

I was running into an issue where i wanted more information from the
TwoFactorChallengeFailedException. It turns out, the exception could
easily provide both the TwoFactorType AND the authentication code
provided. So 💥!
This commit is contained in:
Haacked
2015-04-19 19:42:13 -07:00
parent 518ae12080
commit 93dd16f829
12 changed files with 155 additions and 54 deletions
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.Serialization;
namespace Octokit
@@ -13,23 +12,26 @@ namespace Octokit
#endif
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors",
Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")]
public class TwoFactorChallengeFailedException : AuthorizationException
public class TwoFactorChallengeFailedException : TwoFactorAuthorizationException
{
/// <summary>
/// Constructs an instance of TwoFactorChallengeFailedException
/// </summary>
public TwoFactorChallengeFailedException() :
base(HttpStatusCode.Unauthorized, null)
public TwoFactorChallengeFailedException() : base(TwoFactorType.None, null)
{
}
/// <summary>
/// Constructs an instance of TwoFactorChallengeFailedException
/// </summary>
/// <param name="authorizationCode">The authorization code that was incorrect</param>
/// <param name="innerException">The inner exception</param>
public TwoFactorChallengeFailedException(Exception innerException)
: base(HttpStatusCode.Unauthorized, innerException)
public TwoFactorChallengeFailedException(
string authorizationCode,
ApiException innerException)
: base(ParseTwoFactorType(innerException), innerException)
{
AuthorizationCode = authorizationCode;
}
public override string Message
@@ -37,9 +39,16 @@ namespace Octokit
get { return "The two-factor authentication code supplied is not correct"; }
}
public string AuthorizationCode { get; private set; }
static TwoFactorType ParseTwoFactorType(ApiException exception)
{
return exception == null ? TwoFactorType.None : Connection.ParseTwoFactorType(exception.HttpResponse);
}
#if !NETFX_CORE
/// <summary>
/// Constructs an instance of TwoFactorChallengeFailedException
/// Constructs an instance of TwoFactorRequiredException.
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo"/> that holds the
@@ -52,6 +61,14 @@ namespace Octokit
protected TwoFactorChallengeFailedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
if (info == null) return;
AuthorizationCode = info.GetString("AuthorizationCode");
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("AuthorizationCode", AuthorizationCode);
}
#endif
}