I have a function like this to create a github repo and return it.
def CreateGitHubRepo(token, repo_name):
# instantiate github account
g = Github(token)
# create authenticated user
authed_user = g.get_user()
# create a new repo
repo = authed_user.create_repo(repo_name)
return repo
However if a repo already exists with the same name I get an error raised github.GithubException.GithubException: 422 {"message": "Repository creation failed.", "errors": [{"resource": "Repository", "code": "custom", "field": "name", "message": "name already exists on this account"}], "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"}
My question is how could I handle this in my script to catch this error and move forward e.g.
try:
NewRepo = CreateGitHubRepo(token, repo_name)
print("Created New Git Repo: %s" % repo_name)
print(NewRepo)
except GithubException as err:
print('test')
I've tried all the ways I can think of to get the except to catch that error and I'm a little confused.
Answer, I wasn't importing the github module as desired. I had
from github import Github
so wasn't including the GithubException class.Lesson, check the imports are doing what you expect.