473,320 Members | 1,823 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,320 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 9785
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: 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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.