mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-13 13:25:26 +00:00
Improve sync error format for children options
This commit is contained in:
parent
4182496713
commit
de5fa8bc9c
@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, TYPE_CHECKING, List, Optional, Union
|
from typing import Any, TYPE_CHECKING, List, Optional, Sequence, Union
|
||||||
|
|
||||||
from ..enums import AppCommandOptionType, AppCommandType, Locale
|
from ..enums import AppCommandOptionType, AppCommandType, Locale
|
||||||
from ..errors import DiscordException, HTTPException, _flatten_error_dict
|
from ..errors import DiscordException, HTTPException, _flatten_error_dict
|
||||||
@ -50,7 +50,7 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .commands import Command, Group, ContextMenu
|
from .commands import Command, Group, ContextMenu, Parameter
|
||||||
from .transformers import Transformer
|
from .transformers import Transformer
|
||||||
from .translator import TranslationContextTypes, locale_str
|
from .translator import TranslationContextTypes, locale_str
|
||||||
from ..types.snowflake import Snowflake, SnowflakeList
|
from ..types.snowflake import Snowflake, SnowflakeList
|
||||||
@ -448,7 +448,18 @@ class MissingApplicationID(AppCommandError):
|
|||||||
super().__init__(message or APP_ID_NOT_FOUND)
|
super().__init__(message or APP_ID_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
def _get_command_error(index: str, inner: Any, commands: List[CommandTypes], messages: List[str]) -> None:
|
def _get_command_error(
|
||||||
|
index: str,
|
||||||
|
inner: Any,
|
||||||
|
objects: Sequence[Union[Parameter, CommandTypes]],
|
||||||
|
messages: List[str],
|
||||||
|
indent: int = 0,
|
||||||
|
) -> None:
|
||||||
|
# Import these here to avoid circular imports
|
||||||
|
from .commands import Command, Group, ContextMenu
|
||||||
|
|
||||||
|
indentation = ' ' * indent
|
||||||
|
|
||||||
# Top level errors are:
|
# Top level errors are:
|
||||||
# <number>: { <key>: <error> }
|
# <number>: { <key>: <error> }
|
||||||
# The dicts could be nested, e.g.
|
# The dicts could be nested, e.g.
|
||||||
@ -461,21 +472,35 @@ def _get_command_error(index: str, inner: Any, commands: List[CommandTypes], mes
|
|||||||
|
|
||||||
idx = int(index)
|
idx = int(index)
|
||||||
try:
|
try:
|
||||||
command = commands[idx]
|
obj = objects[idx]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
dedent_one_level = ' ' * (indent - 2)
|
||||||
errors = _flatten_error_dict(inner, index)
|
errors = _flatten_error_dict(inner, index)
|
||||||
messages.extend(f'In {k}: {v}' for k, v in errors.items())
|
messages.extend(f'{dedent_one_level}In {k}: {v}' for k, v in errors.items())
|
||||||
return
|
return
|
||||||
|
|
||||||
callback = getattr(command, 'callback', None)
|
children: Sequence[Union[Parameter, CommandTypes]] = []
|
||||||
class_name = command.__class__.__name__
|
if isinstance(obj, Command):
|
||||||
if callback:
|
messages.append(f'{indentation}In command {obj.qualified_name!r} defined in function {obj.callback.__qualname__!r}')
|
||||||
messages.append(f'In {class_name} {command.qualified_name!r} defined in {callback.__qualname__!r}')
|
children = obj.parameters
|
||||||
|
elif isinstance(obj, Group):
|
||||||
|
messages.append(f'{indentation}In group {obj.qualified_name!r} defined in module {obj.module!r}')
|
||||||
|
children = obj.commands
|
||||||
|
elif isinstance(obj, ContextMenu):
|
||||||
|
messages.append(
|
||||||
|
f'{indentation}In context menu {obj.qualified_name!r} defined in function {obj.callback.__qualname__!r}'
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
messages.append(f'In {class_name} {command.qualified_name!r} defined in module {command.module!r}')
|
messages.append(f'{indentation}In parameter {obj.name!r}')
|
||||||
|
|
||||||
errors = _flatten_error_dict(inner)
|
for key, remaining in inner.items():
|
||||||
messages.extend(f' {k}: {v}' for k, v in errors.items())
|
# Special case the 'options' key since they have well defined meanings
|
||||||
|
if key == 'options':
|
||||||
|
for index, d in remaining.items():
|
||||||
|
_get_command_error(index, d, children, messages, indent=indent + 2)
|
||||||
|
else:
|
||||||
|
errors = _flatten_error_dict(remaining, key=key)
|
||||||
|
messages.extend(f'{indentation} {k}: {v}' for k, v in errors.items())
|
||||||
|
|
||||||
|
|
||||||
class CommandSyncFailure(AppCommandError, HTTPException):
|
class CommandSyncFailure(AppCommandError, HTTPException):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user