Browse Source

[commands] raise ConversionError on Converter error

This assumes that a Converter class raising non-CommandError
is a programmer error. Makes this type of error easier to
disambiguate from a generic BadArgument.
pull/1441/head
khazhyk 7 years ago
committed by Rapptz
parent
commit
2321ae8d97
  1. 55
      discord/ext/commands/core.py
  2. 15
      discord/ext/commands/errors.py
  3. 3
      docs/ext/commands/api.rst

55
discord/ext/commands/core.py

@ -212,7 +212,7 @@ class Command:
self.instance = instance self.instance = instance
return self return self
async def do_conversion(self, ctx, converter, argument): async def do_conversion(self, ctx, converter, argument, param):
if converter is bool: if converter is bool:
return _convert_to_bool(argument) return _convert_to_bool(argument)
@ -224,21 +224,36 @@ class Command:
if module.startswith('discord.') and not module.endswith('converter'): if module.startswith('discord.') and not module.endswith('converter'):
converter = getattr(converters, converter.__name__ + 'Converter') converter = getattr(converters, converter.__name__ + 'Converter')
if inspect.isclass(converter): try:
if issubclass(converter, converters.Converter): if inspect.isclass(converter):
instance = converter() if issubclass(converter, converters.Converter):
ret = await instance.convert(ctx, argument) instance = converter()
return ret ret = await instance.convert(ctx, argument)
else:
method = getattr(converter, 'convert', None)
if method is not None and inspect.ismethod(method):
ret = await method(ctx, argument)
return ret return ret
elif isinstance(converter, converters.Converter): else:
ret = await converter.convert(ctx, argument) method = getattr(converter, 'convert', None)
return ret if method is not None and inspect.ismethod(method):
ret = await method(ctx, argument)
return ret
elif isinstance(converter, converters.Converter):
ret = await converter.convert(ctx, argument)
return ret
except CommandError as e:
raise e
except Exception as e:
raise ConversionError(converter) from e
try:
return converter(argument)
except CommandError as e:
raise e
except Exception as e:
try:
name = converter.__name__
except AttributeError:
name = converter.__class__.__name__
return converter(argument) raise BadArgument('Converting to "{}" failed for parameter "{}".'.format(name, param.name)) from e
def _get_converter(self, param): def _get_converter(self, param):
converter = param.annotation converter = param.annotation
@ -268,17 +283,7 @@ class Command:
else: else:
argument = quoted_word(view) argument = quoted_word(view)
try: return (await self.do_conversion(ctx, converter, argument, param))
return (await self.do_conversion(ctx, converter, argument))
except CommandError as e:
raise e
except Exception as e:
try:
name = converter.__name__
except AttributeError:
name = converter.__class__.__name__
raise BadArgument('Converting to "{}" failed for parameter "{}".'.format(name, param.name)) from e
@property @property
def clean_params(self): def clean_params(self):

15
discord/ext/commands/errors.py

@ -30,7 +30,7 @@ __all__ = [ 'CommandError', 'MissingRequiredArgument', 'BadArgument',
'NoPrivateMessage', 'CheckFailure', 'CommandNotFound', 'NoPrivateMessage', 'CheckFailure', 'CommandNotFound',
'DisabledCommand', 'CommandInvokeError', 'TooManyArguments', 'DisabledCommand', 'CommandInvokeError', 'TooManyArguments',
'UserInputError', 'CommandOnCooldown', 'NotOwner', 'UserInputError', 'CommandOnCooldown', 'NotOwner',
'MissingPermissions', 'BotMissingPermissions'] 'MissingPermissions', 'BotMissingPermissions', 'ConversionError']
class CommandError(DiscordException): class CommandError(DiscordException):
"""The base exception type for all command related errors. """The base exception type for all command related errors.
@ -49,6 +49,19 @@ class CommandError(DiscordException):
else: else:
super().__init__(*args) super().__init__(*args)
class ConversionError(CommandError):
"""Exception raised when a Converter class raises non-CommandError.
Attributes
----------
converter: :class:`discord.ext.commands.Converter`
The converter that failed.
This inherits from :exc:`.CommandError`.
"""
def __init__(self, converter):
self.converter = converter
class UserInputError(CommandError): class UserInputError(CommandError):
"""The base exception type for errors that involve errors """The base exception type for errors that involve errors
regarding user input. regarding user input.

3
docs/ext/commands/api.rst

@ -187,6 +187,9 @@ Errors
.. autoexception:: discord.ext.commands.CommandError .. autoexception:: discord.ext.commands.CommandError
:members: :members:
.. autoexception:: discord.ext.commands.ConversionError
:members:
.. autoexception:: discord.ext.commands.MissingRequiredArgument .. autoexception:: discord.ext.commands.MissingRequiredArgument
:members: :members:

Loading…
Cancel
Save