[commands] Dispatch command_error on command exec error.

Provide fallback on_command_error - will only fire if no cog handlers and
no local handler.
Propagate exceptions in checks and argument parsing to bot.
This commit is contained in:
Khazhismel Kumykov 2016-06-04 21:21:54 -04:00 committed by Rapptz
parent 329f916e10
commit 33a69681fc
2 changed files with 78 additions and 61 deletions

View File

@ -29,11 +29,12 @@ import discord
import inspect import inspect
import importlib import importlib
import sys import sys
import traceback
from .core import GroupMixin, Command, command from .core import GroupMixin, Command, command
from .view import StringView from .view import StringView
from .context import Context from .context import Context
from .errors import CommandNotFound from .errors import CommandNotFound, CommandError
from .formatter import HelpFormatter from .formatter import HelpFormatter
def _get_variable(name): def _get_variable(name):
@ -247,6 +248,26 @@ class Bot(GroupMixin, discord.Client):
coro = self._run_extra(event, event_name, *args, **kwargs) coro = self._run_extra(event, event_name, *args, **kwargs)
discord.compat.create_task(coro, loop=self.loop) discord.compat.create_task(coro, loop=self.loop)
@asyncio.coroutine
def on_command_error(self, exception, context):
"""|coro|
The default command error handler provided by the bot.
By default this prints to ``sys.stderr`` however it could be
overridden to have a different implementation.
This only fires if you do not specify any listeners for command error.
"""
if self.extra_events.get('on_command_error', None):
return
if hasattr(context.command, "on_error"):
return
print('Ignoring exception in command {}'.format(context.command), file=sys.stderr)
traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr)
# utility "send_*" functions # utility "send_*" functions
def say(self, *args, **kwargs): def say(self, *args, **kwargs):
@ -618,7 +639,12 @@ class Bot(GroupMixin, discord.Client):
command = self.commands[invoker] command = self.commands[invoker]
self.dispatch('command', command, ctx) self.dispatch('command', command, ctx)
ctx.command = command ctx.command = command
try:
yield from command.invoke(ctx) yield from command.invoke(ctx)
except CommandError as e:
command.handle_local_error(e, ctx)
self.dispatch('command_error', e, ctx)
else:
self.dispatch('command_completion', command, ctx) self.dispatch('command_completion', command, ctx)
else: else:
exc = CommandNotFound('Command "{}" is not found'.format(invoker)) exc = CommandNotFound('Command "{}" is not found'.format(invoker))

View File

@ -44,7 +44,10 @@ def inject_context(ctx, coro):
_internal_channel = ctx.message.channel _internal_channel = ctx.message.channel
_internal_author = ctx.message.author _internal_author = ctx.message.author
try:
ret = yield from coro(*args, **kwargs) ret = yield from coro(*args, **kwargs)
except Exception as e:
raise CommandError("Exception raised while executing command") from e
return ret return ret
return wrapped return wrapped
@ -306,7 +309,6 @@ class Command:
@asyncio.coroutine @asyncio.coroutine
def _parse_arguments(self, ctx): def _parse_arguments(self, ctx):
try:
ctx.args = [] if self.instance is None else [self.instance] ctx.args = [] if self.instance is None else [self.instance]
ctx.kwargs = {} ctx.kwargs = {}
args = ctx.args args = ctx.args
@ -350,15 +352,9 @@ class Command:
args.append(transformed) args.append(transformed)
except RuntimeError: except RuntimeError:
break break
except CommandError as e:
self.handle_local_error(e, ctx)
ctx.bot.dispatch('command_error', e, ctx)
return False
return True return True
def _verify_checks(self, ctx): def _verify_checks(self, ctx):
try:
if not self.enabled: if not self.enabled:
raise DisabledCommand('{0.name} command is disabled'.format(self)) raise DisabledCommand('{0.name} command is disabled'.format(self))
@ -367,11 +363,6 @@ class Command:
if not self.can_run(ctx): if not self.can_run(ctx):
raise CheckFailure('The check functions for command {0.name} failed.'.format(self)) raise CheckFailure('The check functions for command {0.name} failed.'.format(self))
except CommandError as exc:
self.handle_local_error(exc, ctx)
ctx.bot.dispatch('command_error', exc, ctx)
return False
return True return True
@asyncio.coroutine @asyncio.coroutine