Skip to content

Commit

Permalink
Merge pull request #27 from ministryofjustice/NIT-854-ldap-cli-add-ex…
Browse files Browse the repository at this point in the history
…ception-handling-and-ensure-appropriate-logging

NIT-854 Add exception handling and add logging where appropriate
  • Loading branch information
andrewmooreio authored Oct 3, 2023
2 parents f8620b4 + 1d94eab commit e219c4f
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 221 deletions.
4 changes: 3 additions & 1 deletion cli/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ def connection():
log.debug("Created database connection successfully")
return conn
except Exception as e:
log.exception(e)
log.exception(
f"Failed to create database connection. An exception of type {type(e).__name__} occurred: {e}"
)
raise e
63 changes: 44 additions & 19 deletions cli/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ def get_access_token(
"Authorization": f"Bearer {jwt_token}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.post(
f"https://api.github.com/app/installations/{installation_id}/access_tokens",
headers=headers,
)
try:
response = requests.post(
f"https://api.github.com/app/installations/{installation_id}/access_tokens",
headers=headers,
)
except Exception as e:
logging.exception(
f"Failed to get access token. An exception of type {type(e).__name__} occurred: {e}"
)
raise e

# extract the token from the response
access_token = response.json().get("token")
return access_token
Expand All @@ -54,25 +61,43 @@ def get_repo(
]
if "@" in url:
logging.info("auth already specified in url")
return Repo.clone_from(
url,
dest_name,
multi_options=multi_options,
)
try:
return Repo.clone_from(
url,
dest_name,
multi_options=multi_options,
)
except Exception as e:
logging.exception(
f"Failed to clone repo. An exception of type {type(e).__name__} occurred: {e}"
)
raise e
# if there is a token, assume auth is required and use the token and auth_type
elif token:
templated_url = f'https://{auth_type}:{token}@{url.split("//")[1]}'
logging.info(f"cloning with token: {templated_url}")
return Repo.clone_from(
templated_url,
dest_name,
multi_options=multi_options,
)
try:
return Repo.clone_from(
templated_url,
dest_name,
multi_options=multi_options,
)
except Exception as e:
logging.exception(
f"Failed to clone repo. An exception of type {type(e).__name__} occurred: {e}"
)
raise e
# if there is no token, assume auth is not required and clone without
else:
logging.info("cloning without auth")
return Repo.clone_from(
url,
dest_name,
multi_options=multi_options,
)
try:
return Repo.clone_from(
url,
dest_name,
multi_options=multi_options,
)
except Exception as e:
logging.exception(
f"Failed to clone repo. An exception of type {type(e).__name__} occurred: {e}"
)
raise e
Loading

0 comments on commit e219c4f

Please sign in to comment.