Scott Chapman
unread,Jan 28, 2012, 9:00:59 PM1/28/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cherrypy-users
Every time I get a database error, I get a traceback from cgitb.
Here's my basic setup:
import cherrypy
import cgitb, sys
def cgitb_display_err():
"""Replace the default error response with an HTML traceback from
cgitb."""
tb = cgitb.html(sys.exc_info())
def set_tb():
cherrypy.response.body = tb
cherrypy.response.headers['Content-Length'] = None
cherrypy.request.hooks.attach('after_error_response', set_tb)
def cgitb_log_err():
"""Log cgitb details to the log."""
tb = cgitb.text(sys.exc_info())
def set_tb():
cherrypy.log(tb)
cherrypy.request.hooks.attach('after_error_response', set_tb)
part of cp_config:
'tools.cgitb_display_err.on': False,
'tools.log_tracebacks.on': False,
'tools.cgitb_log_err.on': True,
cherrypy.tools.cgitb_display_err =
cherrypy.Tool('before_error_response', cgitb_display_err)
cherrypy.tools.cgitb_log_err = cherrypy.Tool('before_error_response',
cgitb_log_err)
cherrypy.engine.subscribe('start_thread', model.db_connect)
cherrypy.quickstart(root, '/', cp_config)
The above turns on the nice cgitb replacement to the standard
traceback logging.
My database model code:
def db_connect(thread_index):
cherrypy.thread_data.db_main = MySQLdb.connect('localhost',
'root', '1etme1n', 'kiosk_mgmt')
cherrypy.thread_data.db_main.autocommit(True)
So when the database does something:
def do_something():
connection = cherrypy.thread_data.db_main
cursor = connection.cursor()
sql = r"""INSERT...."""
params = (a,b,c,....)
cherrypy.log("point a")
cursor.execute(sql, params)
I see "point a" in the log and a traceback. In the current case, I'm
getting a foreign key constraint violation. But the tracebacks in CP's
output are:
[28/Jan/2012:17:29:14] Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 102, in run
hook()
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 62, in __call__
return self.callback(**self.kwargs)
File "C:\Documents and Settings\Scott Chapman\My Documents\devel\BEHR
\server\cp_utils.py", line 16, in cgitb_log_err
tb = cgitb.text(sys.exc_info())
File "C:\Python27\lib\cgitb.py", line 214, in text
formatvalue=lambda value: '=' + pydoc.text.repr(value))
File "C:\Python27\lib\inspect.py", line 885, in formatargvalues
specs.append(strseq(args[i], convert, join))
File "C:\Python27\lib\inspect.py", line 840, in strseq
return convert(object)
File "C:\Python27\lib\inspect.py", line 882, in convert
return formatarg(name) + formatvalue(locals[name])
KeyError: 'connection'
[28/Jan/2012:17:29:14] Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 575, in run
self.respond(pi)
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 676, in respond
self.handle_error()
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 749, in handle_error
self.hooks.run("before_error_response")
File "C:\Python27\lib\site-packages\cherrypy-3.2.2-py2.7.egg\cherrypy
\_cprequest.py", line 112, in run
raise exc
KeyError: 'connection'
The connection is defined in the local scope of the procedure (we
fetch a cursor from it no problem).
I'm not at all clear why this is doing this. Of course this makes
debugging database issues more fun.
Any clues?
Scott