I'm using tg1.1 with sqlalchemy
I'm trying to manage all exceptions using @exception_handler
decorator. It works fine for ValueError, TypeError etc.
But for now I am unable to deal with Database Errors using this same
technique:
here's how the code looks like:
1) Definition of the handler, in model.py, before the Root
controller:
def general_except_handler(self,tg_exceptions=None,**kw):
log.error(u'*'+str(datetime.datetime.now())+u'* '+str
(request.path) + u" Exception: " + unicode(str
(tg_exceptions),'latin'))
flash(u"exception::Exception: " + unicode(str
(tg_exceptions),'latin'))
redirect('/')
return dict()
2) Definition, just below the handler, of the catching expression
catch_excep_expr="isinstance(tg_exceptions, (KeyError,TypeError))"
3) I then use it for the controllers with exception_handler decorator
@expose(template="resyweb.templates.generic_form_display")
@exception_handler(general_except_handler,catch_excep_expr)
@identity.require(identity.in_group("admin"))
def edit_reviewer(self,**kw):
It works fine but when I try to include something like SQLAlchemyError
(previously imported with
from sqlalchemy.exceptions import SQLAlchemyError ), I cannot start
the application:
...
in Root
@identity.require(identity.in_group("admin"))
File "c:\python25\lib\site-packages\TurboGears-1.1-py2.5.egg
\turbogears\errorh
andling.py", line 133, in register
dispatch_error_adaptor(handler or func))
File "C:\Python25\lib\site-packages\decoratortools-1.7-py2.5.egg\peak
\util\dec
orators.py", line 601, in do_decorate
File "build\bdist.win32\egg\peak\rules\core.py", line 197, in
callback
File "build\bdist.win32\egg\peak\rules\core.py", line 190, in
register_for_cla
ss
File "build\bdist.win32\egg\peak\rules\core.py", line 65, in
parse_rule
File "build\bdist.win32\egg\peak\rules\core.py", line 153, in
__call__
File "build\bdist.win32\egg\peak\rules\predicates.py", line 517, in
_parse_str
ing
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 424, in
parse_exp
r
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 51, in
com_binary
File "build\bdist.win32\egg\peak\rules\predicates.py", line 189, in
And
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 217, in
atom
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 51, in
com_binary
File "build\bdist.win32\egg\peak\rules\predicates.py", line 189, in
And
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 217, in
atom
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 178, in
power
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 284, in
com_call_
function
File "build\bdist.win32\egg\peak\rules\predicates.py", line 199, in
CallFunc
File "build\bdist.win32\egg\peak\rules\predicates.py", line 233, in
apply_meta
File "build\bdist.win32\egg\peak\rules\predicates.py", line 213, in
parse
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 217, in
atom
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 356, in
testlist_
gexp
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 51, in
com_binary
File "build\bdist.win32\egg\peak\rules\codegen.py", line 428, in
method
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 419, in
build
File "build\bdist.win32\egg\peak\rules\ast_builder.py", line 10, in
<lambda>
File "build\bdist.win32\egg\peak\rules\codegen.py", line 347, in
Name
NameError: SQLAlchemyError
Would anyone know how to find a solution for this problem, or maybe
another technique for catching application-wide database exceptions?
Thanks
Adrien
I *think* this is because you need to have the name SQLAlchemy defined in the
module you define the handler in. So do you import it in your controller
file?
Diez
Yes (if I understand correcly your point) I import the error class
just before defining the exception-catching expression, at the top of
controllers.py (Outside of the Root controller, and declaring it
inside the Root controller doesn't change anything either):
from sqlalchemy.orm.exc import NoResultFound
catch_excep_expr="isinstance(tg_exceptions,
(KeyError,TypeError,NoResultFound))"
By the way I'm not getting an import error, but a NameError
I'm sorry, I didn't catch the sentence that you already imported the exception
in the first post.
I don't know what to do else, except from making some nasty hack like
"SQLAlchemyError" in tg_exceptions.__class__.__name__
which is of course ugly because it doesn't work for sub-classes.
Other than that, I'd try to find more information about peak rules.
Diez
IntegrityError =model.hub.getConnection
()._dbConnection.module.IntegrityError
ProgrammingError =model.hub.getConnection
()._dbConnection.module.ProgrammingError
from sqlobject import SQLObjectNotFound
from sqlobject.dberrors import DuplicateEntryError
catch_excep_expr="isinstance(tg_exceptions,
(KeyError,SQLObjectNotFound,DuplicateEntryError,IntegrityError,ValueError,ProgrammingError))"
And it worked with no trouble, I wonder what makes it different with
sqlalchemy...
If anyone has another idea about this, or technique to handle
application-wide db errors, I'll be glad to read about it!
Adrien
http://groups.google.com/group/turbogears/browse_thread/thread/9a2dc934bcd535bb
@tg.errorhandling.dispatch_error.when("tg_exceptions is not None")
def exception(self , tg_source, tg_errors, tg_exception, *args,
**kw):
# do some stuff.
return dict(tg_template="myproject.templates.error.500")
this will catch all exceptions inside that controller, you could
distinct between different exception types inside exception() method.
timor