Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions lib50/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,23 +1024,46 @@ def _match_files(universe, pattern):

def get_content(org, repo, branch, filepath):
"""Get all content from org/repo/branch/filepath at GitHub."""
url = "https://github.com/{}/{}/raw/{}/{}".format(org, repo, branch, filepath)
try:
r = requests.get(url)
if not r.ok:
if r.status_code == 404:
raise InvalidSlugError(_("Invalid slug. Did you mean to submit something else?"))
else:
# Check if GitHub outage may be the source of the issue
check_github_status()

# Otherwise raise a ConnectionError
raise ConnectionError(_("Could not connect to GitHub. Do make sure you are connected to the internet."))

except requests.exceptions.SSLError as e:

def _handle_ssl_error(e):
"""Handle SSL errors consistently."""
raise ConnectionError(_(f"Could not connect to GitHub due to a SSL error.\nPlease check GitHub's status at githubstatus.com.\nError: {e}"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More likely TLS nowadays.


def _handle_non_404_error():
"""Handle non-404 HTTP errors consistently."""

return r.content
# Check if GitHub outage may be the source of the issue
check_github_status()

# Otherwise raise a ConnectionError
raise ConnectionError(_("Could not connect to GitHub. Do make sure you are connected to the internet."))

def _make_request(url):
"""Make a request and handle SSL errors."""
try:
return requests.get(url)
except requests.exceptions.SSLError as e:
_handle_ssl_error(e)

url = "https://github.com/{}/{}/raw/{}/{}".format(org, repo, branch, filepath)
r = _make_request(url)

if r.ok:
return r.content

if r.status_code == 404:
# Try fallback URL in case there were issues with github.com's redirect
fallback_url = "https://raw.githubusercontent.com/{}/{}/{}/{}".format(org, repo, branch, filepath)
r = _make_request(fallback_url)

if r.ok:
return r.content
elif r.status_code == 404:
raise InvalidSlugError(_("Invalid slug. Did you mean to submit something else?"))
else:
_handle_non_404_error()
else:
_handle_non_404_error()


def check_github_status():
Expand Down