Added better error reporting to cmdhandler - making a nested function to execute the command tended to hide tracebacks.

This commit is contained in:
Griatch 2015-02-20 20:20:06 +01:00
parent 6edc09411a
commit 6f9042e191

View file

@ -291,56 +291,62 @@ def cmdhandler(called_by, raw_string, _testing=False, callertype="session", sess
deferred (Deferred): this will fire when the func() method deferred (Deferred): this will fire when the func() method
returns. returns.
""" """
# Assign useful variables to the instance try:
cmd.caller = caller # Assign useful variables to the instance
cmd.cmdstring = cmdname cmd.caller = caller
cmd.args = args cmd.cmdstring = cmdname
cmd.cmdset = cmdset cmd.args = args
cmd.sessid = session.sessid if session else sessid cmd.cmdset = cmdset
cmd.session = session cmd.sessid = session.sessid if session else sessid
cmd.player = player cmd.session = session
cmd.raw_string = unformatted_raw_string cmd.player = player
#cmd.obj # set via on-object cmdset handler for each command, cmd.raw_string = unformatted_raw_string
# since this may be different for every command when #cmd.obj # set via on-object cmdset handler for each command,
# merging multuple cmdsets # since this may be different for every command when
# merging multuple cmdsets
if hasattr(cmd, 'obj') and hasattr(cmd.obj, 'scripts'): if hasattr(cmd, 'obj') and hasattr(cmd.obj, 'scripts'):
# cmd.obj is automatically made available by the cmdhandler. # cmd.obj is automatically made available by the cmdhandler.
# we make sure to validate its scripts. # we make sure to validate its scripts.
yield cmd.obj.scripts.validate() yield cmd.obj.scripts.validate()
if _testing: if _testing:
# only return the command instance # only return the command instance
returnValue(cmd) returnValue(cmd)
# assign custom kwargs to found cmd object # assign custom kwargs to found cmd object
for key, val in kwargs.items(): for key, val in kwargs.items():
setattr(cmd, key, val) setattr(cmd, key, val)
# pre-command hook # pre-command hook
abort = yield cmd.at_pre_cmd() abort = yield cmd.at_pre_cmd()
if abort: if abort:
# abort sequence # abort sequence
returnValue(abort) returnValue(abort)
# Parse and execute # Parse and execute
yield cmd.parse() yield cmd.parse()
# main command code # main command code
# (return value is normally None) # (return value is normally None)
ret = yield cmd.func() ret = yield cmd.func()
# post-command hook # post-command hook
yield cmd.at_post_cmd() yield cmd.at_post_cmd()
if cmd.save_for_next: if cmd.save_for_next:
# store a reference to this command, possibly # store a reference to this command, possibly
# accessible by the next command. # accessible by the next command.
caller.ndb.last_cmd = yield copy(cmd) caller.ndb.last_cmd = yield copy(cmd)
else: else:
caller.ndb.last_cmd = None caller.ndb.last_cmd = None
# return result to the deferred # return result to the deferred
returnValue(ret) returnValue(ret)
except Exception:
string = "%s\nAbove traceback is from an untrapped error."
string += " Please file a bug report."
logger.log_trace(_(string))
caller.msg(string % format_exc())
raw_string = to_unicode(raw_string, force_string=True) raw_string = to_unicode(raw_string, force_string=True)