我到处搜索有关CherryPy如何处理异常的信息,但我似乎无法弄清楚为什么我的try / catch块没有被正确调用。我认为问题是在另一个线程上抛出/处理异常,或者至少在我的catch块之前被截获/处理。
在下面的代码中,最底层的catch块永远不会被调用。相反,在我能够处理异常之前,它会使用完整的异常消息向浏览器返回响应。
如何通过cherrypy处理响应之前捕获此异常?
import cherrypy
from cherrypy.process import wspbus, plugins
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String
import os
# BELOW CLASSES COPIED FROM
# https://bitbucket.org/Lawouach/cherrypy-recipes/src/d140e6da973aa271e6b68a8bc187e53615674c5e/web/database/?at=default
class SATool(cherrypy.Tool):
def __init__(self):
"""
The SA tool is responsible for associating a SA session
to the SA engine and attaching it to the current request.
Since we are running in a multithreaded application,
we use the scoped_session that will create a session
on a per thread basis so that you don't worry about
concurrency on the session object itself.
This tools binds a session to the engine each time
a requests starts and commits/rollbacks whenever
the request terminates.
"""
cherrypy.Tool.__init__(self, 'on_start_resource',
self.bind_session,
priority=20)
def _setup(self):
cherrypy.Tool._setup(self)
cherrypy.request.hooks.attach('on_end_resource',
self.commit_transaction,
priority=80)
def bind_session(self):
"""
Attaches a session to the request's scope by requesting
the SA plugin to bind a session to the SA engine.
"""
session = cherrypy.engine.publish('bind-session').pop()
cherrypy.request.db = session
def commit_transaction(self):
"""
Commits the current transaction or rolls back
if an error occurs. Removes the session handle
from the request's scope.
"""
if not hasattr(cherrypy.request, 'db'):
return
cherrypy.request.db = None
cherrypy.engine.publish('commit-session')
class SAEnginePlugin(plugins.SimplePlugin):
def __init__(self, bus):
"""
The plugin is registered to the CherryPy engine and therefore
is part of the bus (the engine *is* a bus) registery.
We use this plugin to create the SA engine. At the same time,
when the plugin starts we create the tables into the database
using the mapped class of the global metadata.
"""
plugins.SimplePlugin.__init__(self, bus)
self.sa_engine = None
self.session = scoped_session(sessionmaker(autoflush=True,
autocommit=False))
def start(self):
self.bus.log('Starting up DB access')
self.sa_engine = create_engine('oracle+cx_oracle://%s:%s@%s' %
(os.getenv('DB_USERNAME'), os.getenv('DB_PASSWORD'), os.getenv('DB_SERVER')),
echo=True)
self.bus.subscribe("bind-session", self.bind)
self.bus.subscribe("commit-session", self.commit)
def stop(self):
self.bus.log('Stopping down DB access')
self.bus.unsubscribe("bind-session", self.bind)
self.bus.unsubscribe("commit-session", self.commit)
if self.sa_engine:
self.sa_engine.dispose()
self.sa_engine = None
def bind(self):
"""
Whenever this plugin receives the 'bind-session' command, it applies
this method and to bind the current session to the engine.
It then returns the session to the caller.
"""
self.session.configure(bind=self.sa_engine)
return self.session
def commit(self):
"""
Commits the current transaction or rollbacks if an error occurs.
In all cases, the current session is unbound and therefore
not usable any longer.
"""
try:
self.session.commit()
except:
self.session.rollback()
raise
finally:
self.session.remove()
# SQL Alchemy model
Base = declarative_base()
class Registration(Base):
__tablename__ = 'beta_registration'
email = Column('email', String, primary_key=True)
class Root():
@cherrypy.expose
def index(self):
registration = Registration()
registration.email = "test@test.com"
db = cherrypy.request.db
try:
db.add(registration)
except Exception as e:
# **** never gets here *****
# should be IntegrityError on second call from sqlalchemy.exc
raise cherrypy.HTTPError("409 Conflict", "The email address has already been registered")
SAEnginePlugin(cherrypy.engine).subscribe()
cherrypy.tools.db = SATool()
cherrypy.config.update({
'tools.sessions.on' : True,
'tools.sessions.storage_type' : 'File',
'tools.sessions.storage_path' : 'adf'
})
cherrypy.quickstart(Root(), '/')
日志:
[09/Apr/2014:16:43:54] ENGINE Error in 'commit-session' listener <bound method SAEnginePlugin.commit of <plugins.saplugin.SAEnginePlugin object at 0x105a0cd10>>
Traceback (most recent call last):
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/process/wspbus.py", line 197, in publish
output.append(listener(*args, **kwargs))
File "/Users/xatter/Sites/connectfor/plugins/saplugin.py", line 57, in commit
self.session.commit()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 149, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 765, in commit
self.transaction.commit()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 370, in commit
self._prepare_impl()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 350, in _prepare_impl
self.session.flush()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1879, in flush
self._flush(objects)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1997, in _flush
transaction.rollback(_capture_exception=True)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 57, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1961, in _flush
flush_context.execute()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 370, in execute
rec.execute(self)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 523, in execute
uow
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
mapper, table, insert)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 562, in _emit_insert_statements
execute(statement, multiparams)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 717, in execute
return meth(self, multiparams, params)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 317, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 814, in _execute_clauseelement
compiled_sql, distilled_params
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 927, in _execute_context
context)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1076, in _handle_dbapi_exception
exc_info
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 185, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 920, in _execute_context
context)
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 425, in do_execute
cursor.execute(statement, parameters)
IntegrityError: (IntegrityError) duplicate key value violates unique constraint "beta_registration_pk"
DETAIL: Key (email)=(test@test.com) already exists.
'INSERT INTO beta_registration (email) VALUES (%(email)s)' {'email': u'test@test.com'}
[09/Apr/2014:16:43:54] Traceback (most recent call last):
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 102, in run
hook()
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 62, in __call__
return self.callback(**self.kwargs)
File "/Users/xatter/Sites/connectfor/plugins/satool.py", line 47, in commit_transaction
cherrypy.engine.publish('commit-session')
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/process/wspbus.py", line 215, in publish
raise exc
ChannelFailures: IntegrityError('(IntegrityError) duplicate key value violates unique constraint "beta_registration_pk"\nDETAIL: Key (email)=(test@test.com) already exists.\n',)
[09/Apr/2014:16:43:54] HTTP
Request Headers:
Content-Length: 25
COOKIE: remember_token=_tm4c-HNpJWTHB1PXj8Wbg; session_id=25757ddac3a84730ce3b87f32b80a4288f5421b4
HOST: localhost:5000
ORIGIN: http://localhost:5000
CONNECTION: keep-alive
Remote-Addr: 127.0.0.1
ACCEPT: application/json, text/plain, */*
USER-AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14
REFERER: http://localhost:5000/
ACCEPT-LANGUAGE: en-us
Content-Type: application/json;charset=UTF-8
ACCEPT-ENCODING: gzip, deflate
[09/Apr/2014:16:43:54] HTTP Traceback (most recent call last):
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 670, in respond
self.hooks.run('on_end_resource')
File "/Users/xatter/Sites/connectfor/venv/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 112, in run
raise exc
ChannelFailures: IntegrityError('(IntegrityError) duplicate key value violates unique constraint "beta_registration_pk"\nDETAIL: Key (email)=(test@test.com) already exists.\n',)
127.0.0.1 - - [09/Apr/2014:16:43:54] "POST /beta_registration HTTP/1.1" 500 1303 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
答案 0 :(得分:1)
问题是,在您尝试提交更改时,SQLAlchemy不会触及数据库,并且在请求结束时您正在使用的SATool提交。这意味着当你的函数已经返回并且try / except已经执行时它会提交。
要捕获这些错误,您必须自己进行提交,这意味着要删除SATool的这种方法:
def _setup(self):
cherrypy.Tool._setup(self)
cherrypy.request.hooks.attach('on_end_resource',
self.commit_transaction,
priority=80)
然后发布到您要提交的引擎:
cherrypy.engine.publish('commit-session')
所以你的Root对象就是这个列表:
class Root(object):
@cherrypy.expose
def index(self):
registration = Registration()
registration.email = "test@test.com"
db = cherrypy.request.db
db.add(registration)
try:
cherrypy.engine.publish('commit-session')
except Exception as e:
raise cherrypy.HTTPError("409 Conflict", "The email address has already been registered")
这意味着你需要做到:
cherrypy.engine.publish('commit-session')
显式修改所有修改db的请求。
您还可以拆分工具以启用/禁用自动提交或仅对工具进行参数化。这可能需要更多细节。如果您想了解更多信息,请发表评论。
答案 1 :(得分:0)
试试这个...除了例外的e
import cherrypy
cherrypy.config.update({
'tools.sessions.on' : True,
'tools.sessions.storage_type' : 'File',
'tools.sessions.storage_path' : 'adf'
})
class Root(object):
@cherrypy.expose
def index(self):
try:
asdf = cherrypy.session['asdf']
except Exception as e:
raise cherrypy.HTTPError("409 Conflict", "The email address has already been registered")
#return str(e)
return 1
@cherrypy.expose
@cherrypy.tools.json_out()
def main(self):
return 'hi'
cherrypy.quickstart(Root())
使用此代码,我看到两个错误。 Cherrypy的自动错误处理和我的客户错误。这是使用python 3.3.3和cherrypy 3.2.4
希望这有帮助!