diff --git a/bin/is b/bin/is index 0fd78b031e76cfdd3f5fa0e3c5ece2f13e1cac08..0268b9687fbeac41042785d430ccf46ca00b7e08 100755 --- a/bin/is +++ b/bin/is @@ -748,10 +748,10 @@ def main(): except KeyboardInterrupt: warn("Keyboard Interrupted") exit(1) - except ISException as e: - error(u"%s.%s" % (e, e.get_tb())) + except ISError as e: + error(exception=e) except Exception as e: - error(u"Unknown error, please report it: %s." % e) + error(u"Unexpected error, please report it with debug enabled", exception=e) # Entry point if __name__ == '__main__': diff --git a/installsystems/exception.py b/installsystems/exception.py index 4887b2652395ee56354a99e348150649115de5af..fd7a959e9db387fd48195f7ed89fc678a01092e8 100644 --- a/installsystems/exception.py +++ b/installsystems/exception.py @@ -32,34 +32,46 @@ class ISException(Exception): ''' Base exception class ''' - def __init__(self, message='', exception=None): - self.message = message + def __init__(self, message="", exception=None): + self.message = unicode(message) self.exception = exception if exception: self.exc_info = sys.exc_info() def __str__(self): if self.exception: - return "%s: %s" % (self.message, self.exception) + return u"%s: %s" % (self.message, self.exception) else: return self.message - def get_tb(self): - if self.exception and installsystems.verbosity > 1: - sio = StringIO.StringIO() + def print_tb(self, fd=sys.stderr): + ''' + Print traceback from embeded exception or current one + ''' + from installsystems.printer import out + # coloring + out("#l##B#", fd=fd, endl="") + # print original exception traceback + if self.exception is not None: traceback.print_exception(self.exc_info[0], self.exc_info[1], - self.exc_info[2], file=sio) - str = "\n#R#%s" % sio.getvalue() - sio.close() - return str - return "" + self.exc_info[2], file=fd) + # print current exception traceback + else: + exc_info = sys.exc_info() + traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], + file=fd) + # reset color + out("#R#", fd=fd, endl="") + class ISError(ISException): ''' Installsystems error; this exception will stop execution ''' + class ISWarning(ISException): ''' Installsystems warning; this exception do not stop execution ''' + diff --git a/installsystems/printer.py b/installsystems/printer.py index fc8a48705fc56f64cf7c93d8e1386c108b667388..0ea6b5b6284d21963a6c06d21c7aaa1266dff09d 100644 --- a/installsystems/printer.py +++ b/installsystems/printer.py @@ -25,6 +25,7 @@ import sys import os import re import installsystems +from installsystems.exception import * NOCOLOR = False @@ -92,10 +93,22 @@ def fatal(message, quit=True, fd=sys.stderr, endl=os.linesep): if quit: os._exit(21) -def error(message, quit=True, fd=sys.stderr, endl=os.linesep): - out(u"#light##red#Error:#reset# #red#%s#reset#" % message, fd, endl) - if sys.exc_info()[0] is not None and installsystems.verbosity > 1: - raise +def error(message=None, exception=None, quit=True, fd=sys.stderr, endl=os.linesep): + # create error message + pmesg = u"" + if message is not None: + pmesg += unicode(message) + if exception is not None: + if pmesg == "": + pmesg += unicode(exception) + else: + pmesg += u": %s" % unicode(exception) + # print error message + if pmesg != "": + out(u"#light##red#Error:#reset# #red#%s#reset#" % pmesg, fd, endl) + # print traceback in debug mode + if installsystems.verbosity > 1 and isinstance(exception, ISException): + exception.print_tb(fd) if quit: exit(42)