[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.
This commit is contained in:
		@@ -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,6 +224,7 @@ 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')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
            if inspect.isclass(converter):
 | 
					            if inspect.isclass(converter):
 | 
				
			||||||
                if issubclass(converter, converters.Converter):
 | 
					                if issubclass(converter, converters.Converter):
 | 
				
			||||||
                    instance = converter()
 | 
					                    instance = converter()
 | 
				
			||||||
@@ -237,8 +238,22 @@ class Command:
 | 
				
			|||||||
            elif isinstance(converter, converters.Converter):
 | 
					            elif isinstance(converter, converters.Converter):
 | 
				
			||||||
                ret = await converter.convert(ctx, argument)
 | 
					                ret = await converter.convert(ctx, argument)
 | 
				
			||||||
                return ret
 | 
					                return ret
 | 
				
			||||||
 | 
					        except CommandError as e:
 | 
				
			||||||
 | 
					            raise e
 | 
				
			||||||
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            raise ConversionError(converter) from e
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
            return converter(argument)
 | 
					            return 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user