Skip to content

Commit c91334b

Browse files
committed
Add InvalidInputError, add better error message for invalid ref in /gh search files
1 parent 736a235 commit c91334b

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

bot/src/ghutils/cogs/app_commands/github.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
from discord.ext.commands import GroupCog
1414
from discord.ui import Button, View
1515
from githubkit import GitHub
16-
from githubkit.exception import GitHubException
16+
from githubkit.exception import GitHubException, RequestFailed
1717
from githubkit.rest import Issue, IssuePropPullRequest, PullRequest, SimpleUser
1818
from more_itertools import consecutive_groups, ilen
1919
from yarl import URL
2020

2121
from ghutils.common.__version__ import VERSION
2222
from ghutils.core.cog import GHUtilsCog, SubGroup
23-
from ghutils.core.types import LoginState, NotLoggedInError
23+
from ghutils.core.types import InvalidInputError, LoginState, NotLoggedInError
2424
from ghutils.db.models import (
2525
UserGitHubTokens,
2626
UserLogin,
@@ -297,14 +297,22 @@ async def files(
297297
if ref is None:
298298
ref = repo.default_branch
299299

300-
tree = await gh_request(
301-
github.rest.git.async_get_tree(
302-
repo.owner.login,
303-
repo.name,
304-
ref,
305-
recursive="1",
300+
try:
301+
tree = await gh_request(
302+
github.rest.git.async_get_tree(
303+
repo.owner.login,
304+
repo.name,
305+
ref,
306+
recursive="1",
307+
)
306308
)
307-
)
309+
except RequestFailed as e:
310+
if e.response.status_code == 404: # pyright: ignore[reportUnknownMemberType]
311+
raise InvalidInputError(
312+
value=ref,
313+
message=f"Ref does not exist in `{repo.full_name}`.",
314+
)
315+
raise
308316

309317
sha = tree.sha[:12]
310318
tree_dict = {item.path: item for item in tree.tree if item.path}

bot/src/ghutils/core/tree.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
TransformerError,
1111
)
1212

13-
from .types import NotLoggedInError, SilentError
13+
from .types import InvalidInputError, NotLoggedInError, SilentError
1414

1515

1616
class GHUtilsCommandTree(CommandTree):
@@ -40,6 +40,14 @@ async def on_error(self, interaction: Interaction, error: AppCommandError):
4040
value=str(value),
4141
inline=False,
4242
)
43+
case InvalidInputError(value=value, message=message):
44+
embed.title = "Invalid input!"
45+
embed.description = message
46+
embed.add_field(
47+
name="Value",
48+
value=str(value),
49+
inline=False,
50+
)
4351
case NotLoggedInError():
4452
embed.title = "Not logged in!"
4553
embed.description = "You must be logged in with GitHub to use this command. Use `/gh login` to log in, then try again."

bot/src/ghutils/core/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from enum import Enum, auto
2+
from typing import Any
23

34
from discord.app_commands import AppCommandError
45

@@ -20,3 +21,15 @@ class NotLoggedInError(AppCommandError):
2021

2122
class SilentError(AppCommandError):
2223
"""Base class for exceptions that should be silently caught and ignored."""
24+
25+
26+
class InvalidInputError(AppCommandError):
27+
"""An exception raised within command handlers when an input value is invalid.
28+
29+
Displays a similar error message as `TransformerError`.
30+
"""
31+
32+
def __init__(self, value: Any, message: str):
33+
self.value = value
34+
self.message = message
35+
super().__init__(f"{message} (value: {value})")

0 commit comments

Comments
 (0)