-
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.
Merge pull request #377 from octokit/haacked/repository-create-except…
…ions Implement RepositoryExistsException
- Loading branch information
Showing
13 changed files
with
304 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Octokit | ||
{ | ||
/// <summary> | ||
/// Exception thrown when creating a repository, but it already exists on the server. | ||
/// </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 RepositoryExistsException : ApiValidationException | ||
{ | ||
string _message; | ||
|
||
/// <summary> | ||
/// Constructs an instance of RepositoryExistsException. | ||
/// </summary> | ||
/// <param name="owner">The login of the owner of the existing repository</param> | ||
/// <param name="name">The name of the existing repository</param> | ||
/// <param name="ownerIsOrganization">True if the owner is an organization</param> | ||
/// <param name="baseAddress">The base address of the repository.</param> | ||
/// <param name="innerException">The inner validation exception.</param> | ||
public RepositoryExistsException( | ||
string owner, | ||
string name, | ||
bool ownerIsOrganization, | ||
Uri baseAddress, | ||
ApiValidationException innerException) | ||
: base(innerException) | ||
{ | ||
Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); | ||
Ensure.ArgumentNotNullOrEmptyString(name, "repositoryName"); | ||
Ensure.ArgumentNotNull(baseAddress, "baseAddress"); | ||
|
||
Owner = owner; | ||
RepositoryName = name; | ||
OwnerIsOrganization = ownerIsOrganization; | ||
var webBaseAddress = baseAddress.Host != GitHubClient.GitHubApiUrl.Host | ||
? baseAddress | ||
: GitHubClient.GitHubDotComUrl; | ||
ExistingRepositoryWebUrl = new Uri(webBaseAddress, new Uri(owner + "/" + name, UriKind.Relative)); | ||
string messageFormat = ownerIsOrganization | ||
? "There is already a repository named '{0}' in the organization '{1}'" | ||
: "There is already a repository named '{0}' for the owner '{1}'."; | ||
|
||
_message = String.Format(CultureInfo.InvariantCulture, messageFormat, name, owner); | ||
} | ||
|
||
/// <summary> | ||
/// The Name of the repository that already exists. | ||
/// </summary> | ||
public string RepositoryName { get; private set; } | ||
|
||
/// <summary> | ||
/// The URL to the existing repository's web page on github.com (or enterprise instance). | ||
/// </summary> | ||
public Uri ExistingRepositoryWebUrl { get; set; } | ||
|
||
/// <summary> | ||
/// A useful default error message. | ||
/// </summary> | ||
public override string Message | ||
{ | ||
get | ||
{ | ||
return _message; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The login of the owner of the repository. | ||
/// </summary> | ||
public string Owner { get; private set; } | ||
|
||
/// <summary> | ||
/// True if the owner is an organization and not the user. | ||
/// </summary> | ||
public bool OwnerIsOrganization { get; private set; } | ||
|
||
#if !NETFX_CORE | ||
protected RepositoryExistsException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
if (info == null) return; | ||
_message = info.GetString("Message"); | ||
RepositoryName = info.GetString("RepositoryName"); | ||
Owner = info.GetString("Owner"); | ||
OwnerIsOrganization = info.GetBoolean("OwnerIsOrganization"); | ||
ExistingRepositoryWebUrl = (Uri)(info.GetValue("ExistingRepositoryWebUrl", typeof(Uri))); | ||
} | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
info.AddValue("Message", Message); | ||
info.AddValue("RepositoryName", RepositoryName); | ||
info.AddValue("Owner", Owner); | ||
info.AddValue("OwnerIsOrganization", OwnerIsOrganization); | ||
info.AddValue("ExistingRepositoryWebUrl", ExistingRepositoryWebUrl); | ||
} | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Linq; | ||
|
||
namespace Octokit | ||
{ | ||
internal static class ApiErrorExtensions | ||
{ | ||
public static string FirstErrorMessageSafe(this ApiError apiError) | ||
{ | ||
if (apiError == null) return null; | ||
if (apiError.Errors == null) return null; | ||
var firstError = apiError.Errors.FirstOrDefault(); | ||
if (firstError == null) return null; | ||
return firstError.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
Oops, something went wrong.