On Aug 7, 2010, at 1:48 PM, Ionuț Arțăriși wrote:

> On 08/07/2010 01:56 PM, Diez B. Roggisch wrote:
>> On Aug 7, 2010, at 12:28 PM, Ionuț Arțăriși wrote:
>> 
>>> Hello,
>>> 
>>> I would like to be able to abandon a request however deeply nested within a 
>>> function and return a json object.
>>> 
>>> pylons.controllers.util.abort is the equivalent of what I would like, but 
>>> it only returns HTML, even if the request was json.
>> I never used that function, but I'd simply create my own decorator that 
>> exposes a passed function as json, and catches a defined exception to render 
>> whatever json you want.
>> 
>> Diez
>> 
> I thought about that, too, but I wish there was a better way to do it than 
> adding another decorator to all of my controller methods.

I don't see the point of that argument, as you need to add a decorator anyway. 
Maybe you misunderstood, what I'm proposing is something like this:


@expose_json
def my_action(...)

So it's one decorator, not two.

Other than that, you can of course always wrap your app into a middleware which 
can catch a defined exception & return whatever you want, based on that 
exception.

Then there is validation, which you can use via standardized error_handlers. We 
do something like this:

@expose("json")
@validate(..., error_handler=json_error_handler)
def my_action(...)


where json_error_handler looks like

def json_error_handler(self):
      response.status = 413
      return tmpl_context.form_errors

(out of my head, can't access the code right now)

I'm currently not really set up for working with TG2 (new machine), so I can't 
comment on anything which works with the original abort. It might be worth 
opening a feature-request ticket, but I'm unsure about the status of TG2.1 
development right now (beta, feature freeez and whatnot)

Diez

-- 
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?hl=en.

Reply via email to