473,288 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Hiding tracebacks from end-users

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.
Oct 23 '07 #1
4 9783
On Oct 23, 7:07 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
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.
No, I think with try-catch statements only. But this is a good
approach and you have nothing to worry about.

Oct 23 '07 #2
On Oct 23, 11:07 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
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.
I think this is a good approach too. But why are you using If
statements for some errors? You should be able to do something like
this:

try:
do something
except AssertionError:
do something
except ZeroDivisionError:
do something
except urllib2.HTTPError:
do something
except...

etc, etc...

If you know what errors to expect, make them into a specific except
block. And leave the catchall on there for the exceptions you've
forgotten.

Mike

Oct 23 '07 #3
On Oct 23, 12:07 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:

....
if expert_mode:
# experts get the full traceback with no hand-holding.
raise
else:
# print a more friendly error message
....

Another approach is to always print a friendly error message,
but append the traceback to a log file (see the traceback
module for how to do that.) The friendly error message can
include a note that full information is available in the log
file.

The advantage of that is that you capture critical information
that is otherwise lost when a non-expert user hits the exception.
For intermittent bugs that are hard to reproduce, this can be
a lifesaver - especially when your favorite non-expert user
seems to have a knack for generating errors that you can't figure
out how to create ;-)

Alan

Oct 23 '07 #4
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrote:
I'm writing a command-line application that is meant to be relatively
user friendly to non-technical users.
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?
There's traceback.format_exception_only().

If the exception value is a unicode object containing characters which
can't be encoded by sys.stderr's encoding, the code above will fail
(give an ugly traceback and fail to display the error message).

format_exception_only() would be less ugly, but it would still fail to
display the error message. So it's safest to check for this case
explicitly and encode with backslashreplace or similar.

In this situation I use bare except and sys.exc_info, rather than
'except Exception', because there are still libraries out there which
raise objects which aren't instances of Exception.

-M-

Oct 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Maxwell Hammer | last post by:
Hope someone can help with a problem I'm having. A python program I wrote terminates with the following traceback. *** start traceback *** Error in atexit._run_exitfuncs: Traceback (most recent...
7
by: Maxwell Hammer | last post by:
Hi all, This is related to an earlier post 'Help with thread related tracebacks'...for which I have had no feedback yet :-( How should a thread complete i.e. how should it exit? Reading the...
0
by: Timothy Smith | last post by:
iw ant to use a singel try except statment with my main app loop in the middle and catch all tracebacks and email them to myself as a bug report. however it seems a little more tricky then i...
6
by: harrylmh | last post by:
Hi, I'm learning C# and I just don't quite understand the need for polymorphism. why do we need to use it? how does a base class variable holding a derived class instance do any good? Also,...
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
0
by: Nathan | last post by:
Hi folks! Throughout my python development career, I've occasionally made various developer tools to show more information about assertions or exceptions with less hassle to the programmer. ...
5
by: George Sakkis | last post by:
I'm reading the docs on sys.exc_info() but I can't tell for sure whether I'm using it safely to get a snapshot of an exception and reraise it later. The use case is a class which acts like a...
11
by: JJ297 | last post by:
I want to hide the Pin field below in my repeater. How do I do this? <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> <tr> <td><font...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
2
Frinavale
by: Frinavale | last post by:
This question may seem a bit newbie-ish but I'm new to desktop applications...please bear with me. I have a function that populates a ComboBox with a bunch of names. To populate it I have to make...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.