"Wrap" a python-cgi
Kalle Svensson
kalle at gnupung.net
Thu Mar 8 14:11:12 EST 2001
Sez Hermann Himmelbauer:
> Hi,
> I am currently using python for several cgi programs. One problem is that
> if my cgi-script has a bug it exits with an exception and the user gets an
> ugly error message done by the webserver.
>
> I would like to put some really simple "bugfree" (if something like this
> can exist :-) "wrapper" around my cgi-scripts, let it trap Exceptions, log
> the error and let it output a nice error page to the user.
>
> My first approach would perhaps be to wrap the whole script into a "try:
> except" clause but at first I don't know if nested try/except clauses are
> allowed and moreover the whole script looks somehow ugly as every line is
> indented.
This is basically a good idea, but you should use functions too. Something
like this might work (untested):
def main():
# whatever goes here...
if __name__ == "__main__":
print "Content-Type: text/html\n"
try:
main()
except:
import traceback, sys
print "<h1>Error!</h1>\n<pre>"
traceback.print_exc(file=sys.stdout)
print "</pre>"
And yes, try: ... except: blocks can be nested.
Peace,
Kalle
--
Email: kalle at gnupung.net | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
[Not signed due to braindamage. Blame Microsoft Outlook Express.]
More information about the Python-list
mailing list