On Sep 27, 2006, at 2:38 PM, Timur Izhbulatov wrote:
>
> On Wed, Sep 27, 2006 at 01:29:05PM +0200, Alberto Valverde wrote:
>>
>> On Sep 27, 2006, at 1:10 PM, Timur Izhbulatov wrote:
>>
>>>
>>> Hi,
>>>
>>> I got a problem using @exception_handler together with Cheetah.
>>>
>>> Consider a situation:
>>>
>>> # in cheetah1/controllers.py
>>> from turbogears import controllers, expose, exception_handler
>>>
>>> class Root(controllers.RootController):
>>> def get_data(self):
>>> raise ValueError('Error!')
>>> return dict(a=1)
>>>
>>> def error(self, tg_exceptions=None):
>>> return dict(exception=str(tg_exceptions))
>>>
>>> @exception_handler(error, rules="True")
>>> @expose(template="cheetah:cheetah1.templates.site")
>>> def index(self):
>>> data = self.get_data()
>>> return data
>>>
>>> # in cheetah1/templates/site.tmpl
>>> a: $a
>>>
>>> I expect @exception_handler to handle the ValueError exception
>>> before @expose
>>> renders the template. But it looks like @expose is called before
>>> @exception_handler and Cheetah.Template.NotFound raises. In this
>>> situation I
>>> have to handle the exception in the method body and change the
>>> template
>>> dynamicaly:
>>>
>>> @expose(template="cheetah:cheetah1.templates.site")
>>> def index(self):
>>> try:
>>> data = self.get_data()
>>> except ValueError, error:
>>> return (dict
>>> (tg_template='cheetah:cheetah1.templates.error',
>>> error=error))
>>> return data
>>>
>>> But @exception_handler is not used and I lose all the benefits it
>>> gives! Is it
>>> posible to solve this problem without writing custom template
>>> plugin/decorator/whatever?
>>>
>>> Thanks in advance,
>>
>> errorhandling only catches exceptions raised *inside* controller
>> methods (well, and SA's run_with_transaction to catch db backend
>> exceptions). The exception from Cheetah is raised somewhere inside
>> expose so errorhandling can't help you there, sorry :(
>
> Yes, but the ValueError exception is raised inside my method. I
> mean this is an
> ordinary sutiation for the MVC pattern when Controller tries to get
> some
> business data from Model and tells View to either render the page
> with data or
> show an error page. And if Controller fails do get the data from
> Model, View
> should not try to render the page.
>
> Apllying the above logic to my example assumes that the ValueError
> should be
> handled before View attempts to render the page.
>
> This code
> @expose(template="cheetah:cheetah1.templates.site")
> def index(self):
> data = self.get_data()
> return data
>
> works as expected. It raises an exception. I try to handle it using
> @exception_handler and get another exception raised inside @expose.
>
> Why is that happening? Is it happening because TG attempts to
> render the page
> before doing error handling? No! I can see errorhandling.try_call
> called from
> controllers._execute_func to get controller method output. So why
> the ValueError
> exception is not handled there?..
>
> Maybe I'm missing something? Could you please shed some light on
> the situation?
Sorry, I had read you wrong. I assumed you wanted to trap the
exception raised by Cheetah, not the ValueError raised inside your
controller method.
Studying it in more detail I think I've found the cause:
When you return a dict from "error", this dict is returned instead of
the dict "index" would have normally returned and passed thorugh
*index*'s expose, therefore yielding the exception you're getting
because the template defined by that expose doesn't exist.
Your use-case could be accomplished if you exposed "error" (perhaps
with a custom error template) so it returns a string therefore
bypassing index's expose's template loading. something like
@expose(template='cheetah:cheetah1.templates.error')
def error(self, tg_exceptions=None):
return dict(exception=str(tg_exceptions))
Sorry for misleading you before in my prev. post.
HTH,
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
-~----------~----~----~----~------~----~------~--~---