diff --git a/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs b/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs index d021995f..94e6387b 100644 --- a/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs @@ -238,8 +238,7 @@ public class PullRequestsClientTests : IDisposable var merge = new MergePullRequest { Sha = fakeSha }; var ex = await Assert.ThrowsAsync(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge)); - //merge exceptions don't inherit from ApiException so ApiError is not available - Assert.True(ex.Message.StartsWith("The merge operation specified a SHA which didn't match")); + Assert.True(ex.Message.StartsWith("Head branch was modified")); } [IntegrationTest] @@ -257,8 +256,7 @@ public class PullRequestsClientTests : IDisposable var merge = new MergePullRequest { Sha = pullRequest.Head.Sha }; var ex = await Assert.ThrowsAsync(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge)); - //merge exceptions don't inherit from ApiException so ApiError is not available - Assert.True(ex.Message.Equals("The pull request is not in a mergeable state")); + Assert.True(ex.Message.Equals("Pull Request is not mergeable")); } [IntegrationTest] diff --git a/Octokit/Clients/PullRequestsClient.cs b/Octokit/Clients/PullRequestsClient.cs index b650be46..b73da1fb 100644 --- a/Octokit/Clients/PullRequestsClient.cs +++ b/Octokit/Clients/PullRequestsClient.cs @@ -124,12 +124,12 @@ namespace Octokit { if (ex.StatusCode == HttpStatusCode.MethodNotAllowed) { - throw new PullRequestNotMergeableException(); + throw new PullRequestNotMergeableException(ex.HttpResponse); } if (ex.StatusCode == HttpStatusCode.Conflict) { - throw new PullRequestMismatchException(); + throw new PullRequestMismatchException(ex.HttpResponse); } throw; diff --git a/Octokit/Exceptions/PullRequestMismatchException.cs b/Octokit/Exceptions/PullRequestMismatchException.cs index 8a77d6dc..f16504b5 100644 --- a/Octokit/Exceptions/PullRequestMismatchException.cs +++ b/Octokit/Exceptions/PullRequestMismatchException.cs @@ -1,4 +1,7 @@ using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Net; using System.Runtime.Serialization; namespace Octokit @@ -10,28 +13,39 @@ namespace Octokit #if !NETFX_CORE [Serializable] #endif - public class PullRequestMismatchException : Exception + [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", + Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")] + public class PullRequestMismatchException : ApiException { - public PullRequestMismatchException() - : base("The merge operation specified a SHA which didn't match " + - "the SHA of the pull request's HEAD") + /// + /// Constructs an instace of . + /// + /// + public PullRequestMismatchException(IResponse response) : this(response, null) { } - public PullRequestMismatchException(string message) - : base(message) + /// + /// Constructs an instance of . + /// + /// The HTTP payload from the server + /// The inner exception + public PullRequestMismatchException(IResponse response, Exception innerException) + : base(response, innerException) { + Debug.Assert(response != null && response.StatusCode == HttpStatusCode.Conflict, + "PullRequestMismatchException created with the wrong HTTP status code"); } - - public PullRequestMismatchException(string message, Exception innerException) - : base(message, innerException) + public override string Message { + //https://developer.github.com/v3/pulls/#response-if-sha-was-provided-and-pull-request-head-did-not-match + get { return ApiErrorMessageSafe ?? "Head branch was modified. Review and try the merge again."; } } #if !NETFX_CORE /// - /// Constructs an instance of PullRequestNotMergeableException. + /// Constructs an instance of . /// /// /// The that holds the @@ -47,6 +61,4 @@ namespace Octokit } #endif } - - } diff --git a/Octokit/Exceptions/PullRequestNotMergeableException.cs b/Octokit/Exceptions/PullRequestNotMergeableException.cs index e24c2d36..ca4143dc 100644 --- a/Octokit/Exceptions/PullRequestNotMergeableException.cs +++ b/Octokit/Exceptions/PullRequestNotMergeableException.cs @@ -1,4 +1,7 @@ using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Net; using System.Runtime.Serialization; namespace Octokit @@ -10,26 +13,39 @@ namespace Octokit #if !NETFX_CORE [Serializable] #endif - public class PullRequestNotMergeableException : Exception + [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", + Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")] + public class PullRequestNotMergeableException : ApiException { - public PullRequestNotMergeableException() - : base("The pull request is not in a mergeable state") + /// + /// Constructs an instance of the class. + /// + /// The HTTP payload from the server + public PullRequestNotMergeableException(IResponse response) : this(response, null) { } - public PullRequestNotMergeableException(string message) - : base(message) + /// + /// Constructs an instance of the class. + /// + /// The HTTP payload from the server + /// The inner exception + public PullRequestNotMergeableException(IResponse response, Exception innerException) + : base(response, innerException) { + Debug.Assert(response != null && response.StatusCode == HttpStatusCode.MethodNotAllowed, + "PullRequestNotMergeableException created with the wrong HTTP status code"); } - public PullRequestNotMergeableException(string message, Exception innerException) - : base(message, innerException) + public override string Message { + //https://developer.github.com/v3/pulls/#response-if-merge-cannot-be-performed + get { return ApiErrorMessageSafe ?? "Pull Request is not mergeable"; } } #if !NETFX_CORE /// - /// Constructs an instance of PullRequestNotMergeableException. + /// Constructs an instance of . /// /// /// The that holds the