Skip to content

Commit

Permalink
Improve error handling in case pull request does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Aug 22, 2018
1 parent 808b344 commit 55368f1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/Cake.Tfs/PullRequest/TfsPullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class TfsPullRequest
/// </summary>
/// <param name="log">The Cake log context.</param>
/// <param name="settings">Settings for accessing TFS.</param>
/// <exception cref="TfsException">If <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
/// <exception cref="TfsPullRequestNotFoundException">If <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
/// is set to <c>true</c> and no pull request could be found.</exception>
public TfsPullRequest(ICakeLog log, TfsPullRequestSettings settings)
{
Expand Down Expand Up @@ -90,7 +90,7 @@ public TfsPullRequest(ICakeLog log, TfsPullRequestSettings settings)
{
if (this.settings.ThrowExceptionIfPullRequestCouldNotBeFound)
{
throw new TfsException("Could not find pull request");
throw new TfsPullRequestNotFoundException("Pull request not found");
}

this.log.Warning("Could not find pull request");
Expand Down Expand Up @@ -141,7 +141,7 @@ public TfsPullRequest(ICakeLog log, TfsPullRequestSettings settings)
/// Returns <see cref="Guid.Empty"/> if no pull request could be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>false</c>.
/// </summary>
/// <exception cref="TfsException">If pull request could not be found and
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
public Guid RepositoryId
{
Expand All @@ -161,7 +161,7 @@ public Guid RepositoryId
/// Returns 0 if no pull request could be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>false</c>.
/// </summary>
/// <exception cref="TfsException">If pull request could not be found and
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
public int PullRequestId
{
Expand All @@ -181,7 +181,7 @@ public int PullRequestId
/// Returns 0 if no pull request could be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>false</c>.
/// </summary>
/// <exception cref="TfsException">If pull request could not be found and
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
public int CodeReviewId
{
Expand All @@ -201,7 +201,7 @@ public int CodeReviewId
/// Returns <see cref="string.Empty"/> if no pull request could be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>false</c>.
/// </summary>
/// <exception cref="TfsException">If pull request could not be found and
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
public string LastSourceCommitId
{
Expand All @@ -220,6 +220,8 @@ public string LastSourceCommitId
/// Votes for the pullrequest.
/// </summary>
/// <param name="vote">The vote for the pull request.</param>
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
public void Vote(TfsPullRequestVote vote)
{
if (!this.ValidatePullRequest())
Expand Down Expand Up @@ -249,9 +251,9 @@ public void Vote(TfsPullRequestVote vote)
/// the pull request instance can be null for subsequent calls.
/// </summary>
/// <returns>True if a valid pull request instance exists.</returns>
/// <exception cref="TfsException">If <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
/// <exception cref="TfsPullRequestNotFoundException">If <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
/// is set to <c>true</c> and no pull request could be found.</exception>
public bool ValidatePullRequest()
private bool ValidatePullRequest()
{
if (this.HasPullRequestLoaded)
{
Expand All @@ -260,7 +262,7 @@ public bool ValidatePullRequest()

if (this.settings.ThrowExceptionIfPullRequestCouldNotBeFound)
{
throw new TfsException("Could not find pull request");
throw new TfsPullRequestNotFoundException("Pull request not found");
}

this.log.Verbose("Skipping, since no pull request instance could be found.");
Expand Down
52 changes: 52 additions & 0 deletions src/Cake.Tfs/PullRequest/TfsPullRequestNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Cake.Tfs.PullRequest
{
using System;
using System.Runtime.Serialization;

/// <summary>
/// Represents an error if a pull request was not found.
/// </summary>
[Serializable]
public class TfsPullRequestNotFoundException : TfsException
{
/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequestNotFoundException"/> class.
/// </summary>
public TfsPullRequestNotFoundException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequestNotFoundException"/> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error. </param>
public TfsPullRequestNotFoundException(string message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequestNotFoundException"/> class with a specified error message
/// and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null
/// reference if no inner exception is specified.</param>
public TfsPullRequestNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequestNotFoundException"/> class with serialized data.
/// </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 TfsPullRequestNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
7 changes: 4 additions & 3 deletions src/Cake.Tfs/TfsAliases.PullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static partial class TfsAliases
/// <returns>Description of the pull request.
/// Returns null if pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>false</c>.</returns>
/// <exception cref="TfsException">If pull request could not be found and
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
[CakeMethodAlias]
[CakeAliasCategory("Pull Request")]
Expand Down Expand Up @@ -78,6 +78,8 @@ public static TfsPullRequest TfsPullRequest(
/// ]]>
/// </code>
/// </example>
/// <exception cref="TfsPullRequestNotFoundException">If pull request could not be found and
/// <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/> is set to <c>true</c>.</exception>
[CakeMethodAlias]
[CakeAliasCategory("Pull Request")]
public static void TfsPullRequestVote(
Expand All @@ -88,8 +90,7 @@ public static void TfsPullRequestVote(
context.NotNull(nameof(context));
settings.NotNull(nameof(settings));

var pullRequest = new TfsPullRequest(context.Log, settings);
pullRequest.Vote(vote);
new TfsPullRequest(context.Log, settings).Vote(vote);
}
}
}

0 comments on commit 55368f1

Please sign in to comment.