summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-03 02:34:49 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-03 02:34:49 -0500
commit42c07c06ab572ed6a071bbb542b7c2a660b67710 (patch)
treec1d53aeca9234ddcf62634f6c372f16eb61d24b8 /cmd2.py
parentf5a425342704bca27f081c973a3625bce3770d6b (diff)
downloadcmd2-git-42c07c06ab572ed6a071bbb542b7c2a660b67710.tar.gz
Exceptions are now much more apparent when they occur to avoid confusion.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/cmd2.py b/cmd2.py
index 84d88b46..4ad27e3a 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -495,10 +495,19 @@ class Cmd(cmd.Cmd):
if msg[-1] != '\n':
self.stdout.write('\n')
- def perror(self, errmsg, statement=None):
+ def perror(self, errmsg, exception_type=None):
if self.debug:
traceback.print_exc()
- print(str(errmsg))
+
+ if exception_type is None:
+ err = self.colorize("ERROR: {}'\n".format(errmsg), 'red')
+ sys.stderr.write(err)
+ else:
+ err = "EXCEPTION of type '{}' occured with message: '{}'\n".format(exception_type, errmsg)
+ sys.stderr.write(self.colorize(err, 'red'))
+
+ war = "To enable full traceback, run the following command: 'set debug true'\n"
+ sys.stderr.write(self.colorize(war, 'yellow'))
def pfeedback(self, msg):
"""For printing nonessential feedback. Can be silenced with `quiet`.
@@ -526,7 +535,9 @@ class Cmd(cmd.Cmd):
'red': {True: '\x1b[31m', False: '\x1b[39m'},
'magenta': {True: '\x1b[35m', False: '\x1b[39m'},
'green': {True: '\x1b[32m', False: '\x1b[39m'},
- 'underline': {True: '\x1b[4m', False: '\x1b[24m'}}
+ 'underline': {True: '\x1b[4m', False: '\x1b[24m'},
+ 'yellow': {True: '\x1b[33m', False: '\x1b[39m'},
+ }
colors = (platform.system() != 'Windows')
def colorize(self, val, color):
@@ -902,8 +913,8 @@ class Cmd(cmd.Cmd):
self.restore_output(statement)
except EmptyStatement:
return 0
- except Exception as e:
- self.perror(str(e), statement)
+ except Exception as ex:
+ self.perror(ex, type(ex).__name__)
finally:
return self.postparsing_postcmd(stop)