mirror of
https://github.com/samba-team/samba.git
synced 2025-07-16 20:59:12 +03:00
s4-samba-tool: improved exception handling in samba-tool
we now do reasonable printing on a wide range of common exception classes, and always force a backtrace on an exception if the debug level is >= 3 Pair-Programmed-With: Jelmer Vernooij <jelmer@samba.org>
This commit is contained in:
@ -17,9 +17,10 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
import optparse
|
||||
import optparse, samba
|
||||
from samba import getopt as options
|
||||
import sys
|
||||
from ldb import LdbError
|
||||
import sys, traceback
|
||||
|
||||
|
||||
class Option(optparse.Option):
|
||||
@ -52,6 +53,40 @@ class Command(object):
|
||||
ret += " " + " ".join([x.upper() for x in self.takes_args])
|
||||
return ret
|
||||
|
||||
def show_command_error(self, e):
|
||||
'''display a command error'''
|
||||
if isinstance(e, CommandError):
|
||||
(etype, evalue, etraceback) = e.exception_info
|
||||
inner_exception = e.inner_exception
|
||||
message = e.message
|
||||
force_traceback = False
|
||||
else:
|
||||
(etype, evalue, etraceback) = sys.exc_info()
|
||||
inner_exception = e
|
||||
message = "uncaught exception"
|
||||
force_traceback = True
|
||||
|
||||
if isinstance(inner_exception, LdbError):
|
||||
(ldb_ecode, ldb_emsg) = inner_exception
|
||||
print >>sys.stderr, "ERROR(ldb): %s - %s" % (message, ldb_emsg)
|
||||
elif isinstance(inner_exception, AssertionError):
|
||||
print >>sys.stderr, "ERROR(assert): %s" % message
|
||||
force_traceback = True
|
||||
elif isinstance(inner_exception, RuntimeError):
|
||||
print >>sys.stderr, "ERROR(runtime): %s - %s" % (message, evalue)
|
||||
elif type(inner_exception) is Exception:
|
||||
print >>sys.stderr, "ERROR(exception): %s - %s" % (message, evalue)
|
||||
force_traceback = True
|
||||
elif inner_exception is None:
|
||||
print >>sys.stderr, "ERROR: %s" % (message)
|
||||
else:
|
||||
print >>sys.stderr, "ERROR(%s): %s - %s" % (str(etype), message, evalue)
|
||||
force_traceback = True
|
||||
|
||||
if force_traceback or samba.get_debug_level() >= 3:
|
||||
traceback.print_tb(etraceback)
|
||||
|
||||
|
||||
synopsis = property(_get_synopsis)
|
||||
|
||||
outf = sys.stdout
|
||||
@ -97,8 +132,8 @@ class Command(object):
|
||||
return -1
|
||||
try:
|
||||
return self.run(*args, **kwargs)
|
||||
except CommandError, e:
|
||||
print >>sys.stderr, "ERROR: %s" % e
|
||||
except Exception, e:
|
||||
self.show_command_error(e)
|
||||
return -1
|
||||
|
||||
def run(self):
|
||||
@ -130,7 +165,11 @@ class SuperCommand(Command):
|
||||
|
||||
|
||||
class CommandError(Exception):
|
||||
pass
|
||||
'''an exception class for netcmd errors'''
|
||||
def __init__(self, message, inner_exception=None):
|
||||
self.message = message
|
||||
self.inner_exception = inner_exception
|
||||
self.exception_info = sys.exc_info()
|
||||
|
||||
|
||||
commands = {}
|
||||
|
Reference in New Issue
Block a user