Hiding tracebacks from end-users
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Tue Oct 23 12:07:32 EDT 2007
I'm writing a command-line application that is meant to be relatively
user friendly to non-technical users.
(Some wags might like to say that "user friendly" and "command-line
application" are, by definition, contradictory. I disagree.)
Consequently, I'd like to suppress Python's tracebacks if an error does
occur, replacing it with a more friendly error message. I'm doing
something like this:
try:
setup()
do_something_useful()
except KeyboardInterrupt:
print >>sys.stderr, "User cancelled"
sys.exit(2)
except Exception, e:
if expert_mode:
# experts get the full traceback with no hand-holding.
raise
else:
# print a more friendly error message
if isinstance(e, AssertionError):
msg = "An unexpected program state occurred"
elif isinstance(e, urllib2.HTTPError):
msg = "An Internet error occurred"
else:
# catch-all for any other exception
msg = "An error occurred"
print>>sys.stderr, msg
print>>sys.stderr, e
sys.exit(1)
else:
sys.exit(0)
Is this a good approach? Is there another way to suppress the traceback
and just print the error message?
--
Steven.
More information about the Python-list
mailing list