Skip to content

Commit

Permalink
Fixed error handling for !lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredMind2 committed Nov 5, 2024
1 parent 4954dd8 commit 302864d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
17 changes: 6 additions & 11 deletions cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ async def lesson(self, ctx: commands.Context, repo: str = "", lesson: str = ""):
lesson = repo
repo = ""

err_code, repo, lesson = github_client.find_lesson_ressource(repo, lesson)
err_code, res = github_client.get_repo_readme(repo, lesson)
try:
repo, lesson = github_client.find_lesson_ressource(repo, lesson)
res = github_client.get_repo_readme(repo, lesson)
except Exception as e:
await self.bot.get_channel(DEBUG).send(str(res))


if err_code == 0:
else:
emb = embed_lesson(res).set_thumbnail(url="attachment://INSAlgo.png")
logo = discord.File("data/INSAlgo.png", filename="INSAlgo.png")
channel = self.bot.get_channel(RESSOURCES)
Expand All @@ -63,13 +65,6 @@ async def lesson(self, ctx: commands.Context, repo: str = "", lesson: str = ""):
assert isinstance(channel, discord.TextChannel)
await channel.send(file=logo, embed=emb)

else :
if err_code == 5 :
res += "\nYou can also pass the exact repo name as an argument of this function."
if err_code == 6 :
res += "\nYou can also pass the exact lesson (folder) name as an argument of this function."

await self.bot.get_channel(DEBUG).send(res)


@commands.command(aliases=["down"])
Expand Down
26 changes: 13 additions & 13 deletions utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,23 @@ def get_repo_readme(self, repo: str, sub_dir: str = "") -> str :
path = f"repos/INSAlgo/{repo}/contents/README.md"

if not self.get(path) :
return 3, f"cannot connect to GitHub API through `api.github.com/{path}`"
raise Exception(f"cannot connect to GitHub API through `api.github.com/{path}`")

resp = self.lr_response()

if self.lr_status_code() == 403 :
return 2, self.get_api_rate()[1]
raise Exception(self.get_api_rate()[1])

if self.lr_status_code() != 200 :
return 3, f"response status code not OK : {self.lr_status_code()}"
raise Exception(f"response status code not OK : {self.lr_status_code()}")

try :
raw_text = b64dcd(resp["content"]).decode("utf-8")

return 0, raw_text
return raw_text

except Exception as err:
return 4, f"could not decode file : {err}"
raise Exception(f"could not decode file : {err}")


# Methods to get the ressources for a lesson.
Expand Down Expand Up @@ -248,9 +248,9 @@ def get_INSAlgo_lessons(self, repo: str) -> tuple[int, str | list[str]] :

def find_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int, str] :
err_code, res = self.get_INSAlgo_repos()

if err_code > 0 :
return 5, f"Could not get INSAlgo's repos :\n{res}"
raise Exception(f"Could not get INSAlgo's repos :\n{res}")

if repo == "" :
cur_year = datetime.today().year
Expand All @@ -259,7 +259,7 @@ def find_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int,
if cur_name not in res :
cur_name = f"INSAlgo-{cur_year-1}-{cur_year}"
if cur_name not in res :
return 5, "Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`."
raise Exception("Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`.")

repo = cur_name
else:
Expand All @@ -275,14 +275,14 @@ def find_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int,
break

if not found:
return 5, "Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`."
raise Exception("Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`.")

# if lesson == "" :

err_code, res = self.get_INSAlgo_lessons(repo)

if err_code > 0 :
return 6, f"Could not get lessons from `{repo}` :\n{res}"
raise Exception(f"Could not get lessons from `{repo}` :\n{res}")

if lesson == "":
best = 0
Expand All @@ -307,6 +307,6 @@ def find_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int,
if not found:
raise Exception(f"No valid lesson name found in `{repo}`. Check `https://github.com/INSAlgo/INSAlgo-2022-2023` for reference.")

return 0, repo, lesson
return repo, lesson

github_client = GithubClient()

0 comments on commit 302864d

Please sign in to comment.