473,231 Members | 1,367 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,231 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 9781
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
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
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.