On Aug 19, 2006, at 9:24 AM, Sanjay wrote:

>
> Hi Alberto,
>
> Thanks for such an overwhelming response and support!

No problem ;)

> But, with my terrible ignorance on python and TG internals, I could  
> not
> succeed trying your example. Got this error:
>
> Traceback (most recent call last):
>   File "./start-ierr.py", line 26, in ?
>     from ierr.controllers import Root
>   File "/Data/LearnTG/ierr/ierr/controllers.py", line 14, in ?
>     class Root(controllers.RootController):
>   File "/Data/LearnTG/ierr/ierr/controllers.py", line 30, in Root
>     @errorhandling.dispatch_error.when("isinstance(tg_exceptions,
> IntegrityError)")
>   File
> "/usr/lib/python2.4/site-packages/TurboGears-0.9a7-py2.4.egg/ 
> turbogears/genericfunctions.py",
> line 20, in when
>     return self._decorate(cond, "primary%d" % order)
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/functions.py",
> line 577, in _decorate
>     cond = self.parseRule(cond,frame=frame) or cond
>   File "<string>", line 10, in parseRule
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/functions.py",
> line 440, in parseRule
>     return self.parse(rule, frame.f_locals, frame.f_globals)
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/functions.py",
> line 326, in parse
>     return parse_expr(expr_string,builder)
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 383, in parse_expr
>     return build(builder, parser.expr(expr).totuple(1)[1])
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 378, in build
>     return production[nodelist[0]](builder,nodelist)
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 178, in power
>     return com_call_function(builder,nodelist,node[2])
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 284, in com_call_function
>     return builder.CallFunc(primaryNode, args, kw, star_node,
> dstar_node)
>   File "build/bdist.linux-i686/egg/dispatch/predicates.py", line 545,
> in method
>   File "build/bdist.linux-i686/egg/dispatch/predicates.py", line 170,
> in CallFunc
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 378, in build
>     return production[nodelist[0]](builder,nodelist)
>   File
> "/usr/lib/python2.4/site-packages/RuleDispatch-0.5a0.dev_r2115- 
> py2.4-linux-i686.egg/dispatch/ast_builder.py",
> line 10, in <lambda>
>     _name   = lambda builder,nodelist: builder.Name(nodelist[1])
>   File "build/bdist.linux-i686/egg/dispatch/predicates.py", line  
> 58, in
> Name
> NameError: IntegrityError

You need to import the exception raised by SQLAlchemy into the  
namespace the dispatch rule (the string passed as an argument to  
"dispatch_error.when") is defined or else you'll get the NameError  
you're getting. It should read:

from sqlalchemy.exceptions import SQLError
...
@errorhandling.dispatch_error.when("isinstance(tg_exceptions,  
SQLError)")
def handle_SQLError(....):
        .....

See how SQLError needs to be defined in the same namespace the rule  
is defined in. One note, I believe watching for a SQLError is more  
portable than watching for a IntegrityError because the later is DB  
driver specific while the former is a SA exception.

> Then, trying to diagnose the error by simplification, I tried this
> code:
>
>     @errorhandling.dispatch_error.when("True")
>     def handle_IE(controller, tg_source, tg_errors, tg_exceptions,
> *args, **kw):
>         log.debug("An IntegrityError (%s) ocurred in method %r of
> controller %r" %
>               tg_exceptions, tg_source, controller)
>         turbogears.flash("An integrity error ocurred")
>
> But, then got this error while execution:
>
> 500 Internal error
>
> The server encountered an unexpected condition which prevented it from
> fulfilling the request.
>
> Page handler: <bound method Root.createsanjay of  
> <ierr.controllers.Root
> object at 0xb767cc0c>>
> Traceback (most recent call last):
>   File
> "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/ 
> _cphttptools.py",
> line 105, in _run
>     self.main()
>   File
> "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/ 
> _cphttptools.py",
> line 254, in main
>     body = page_handler(*virtual_path, **self.params)
>   File "<string>", line 3, in createsanjay
>   File
> "/usr/lib/python2.4/site-packages/TurboGears-0.9a7-py2.4.egg/ 
> turbogears/controllers.py",
> line 331, in expose
>     output = database.run_with_transaction(
>   File "<string>", line 5, in run_with_transaction
>   File
> "/usr/lib/python2.4/site-packages/TurboGears-0.9a7-py2.4.egg/ 
> turbogears/database.py",
> line 282, in sa_rwt
>     errorhandling.dispatch_error(controller, real_func, None, e,  
> *args,
> **kw)
>   File "<string>", line 5, in dispatch_error
>   File "/Data/LearnTG/ierr/ierr/controllers.py", line 32, in handle_IE
>     log.debug("An IntegrityError (%s) ocurred in method %r of
> controller %r" %
> TypeError: not enough arguments for format string

Ooops, the sample code had a typo, it should read:
log.debug("An IntegrityError (%s) ocurred in method %r of controller % 
r" %
                    (tg_exceptions, tg_source, controller))

(Notice that the arguments for the format string are a tuple)

> Not able to diagnose. I am using 9a7.
>
> Another point concerning me -
>
> The pattern I think to use is someting like this:
>
>     @expose(template="tm.templates.form")
>     def edit_person(self, tg_errors=None, tg_exceptions=None):
>         if tg_errors:
>             flash("Problem validating! Please correct")
>         if tg_exceptions:
>             flash("Problem saving! Please correct")
>         .
>         .
>         .
>
>
>     @expose()
>     @turbogears.validate(form=person_form)
>     @turbogears.error_handler(edit_person)
>     @turbogears.exception_handler(edit_person)
>     def save(self, name):
>             u = User(name=name)
>             u.flush()
>
> I guess this is the most easy way to handle errors and exceptions  
> while
> data entry by user. Need guidence on the way to achieve this pattern
> with the patch.

Actually, the above code should work as-is ;) Just a note, if you  
only want to branch to "edit_person" in case a SQLError exception is  
raised by SA you can specialize exception_handler with a dispatch rule:

from sqlalchemy.exceptions import SQLError
@turbogears.exception_handler(edit_person, "isinstance(tg_exceptions,  
SQLError)")
def save(....):
        ....

It'll probably help to take a look at
http://trac.turbogears.org/turbogears/wiki/HowDoesErrorHandlingWork

Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to