On Sun, May 15, 2011 at 11:27 PM, Chris McDonough <chr...@plope.com> wrote:
> I've created a branch named "httpexception-utils" on GitHub which
> contains an implementation of "redirect" and "abort" for Pyramid that
> act like their Pylons brethren.
>
> In short, the abort feature is used like this:
>
>    from pryamid.httpexceptions import abort
>
>    def aview(request):
>        abort(401)
>
> This will perform the same job as what used to be necessary as:
>
>    def aview(request):
>        return HTTPUnauthorized()
>
> The redirect feature is used like this:
>
>    from pryamid.httpexceptions import redirect
>
>    def aview(request):
>        redirect('http://example.com')
>
> This will perform the same job as what used to be necessary as:
>
>    def aview(request):
>        return HTTPFound(location='http://example.com')
>
> In addition, any "HTTP exception" (an exception imported from
> pyramid.httpexceptions) can now be raised rather than returned.  The
> object raised will be used as a response.
>
> Here's the branch:
>
> https://github.com/Pylons/pyramid/tree/httpexception-utils
>
> Here's the commit which added the feature:
>
> https://github.com/Pylons/pyramid/commit/1ffb8e3cc21603b29ccd78152f82cca7f61a09b1
>
> It'd be nice to get some feedback from existing Pylons users to see if
> the implementations of these features are "good enough"; it'd also be
> nice to hear dissenting opinions with reasons for dissent if folks
> believe this feature should not be added to Pyramid.

Raising HTTP exceptions should definitely be added to Pyramid. It's
silly for an application to have to add an exception view that just
returns the exception: it feels kludgy, it's not what views are for,
and it adds to the overhead. Why can't the code that invokes the view
just have an 'except HTTPException:'?  If the user really wants to
register an exception view in order to display a fancy-dancy page,
that's another thing.  It seems like Pyramid should be able to
accommodate both.

HTTP errors *are* exceptions. If you call a support method and it
discovers that a required query parameter is missing, what's it
supposed to do? The request can't continue, so it might as well kill
it right there. That's directly akin to a ZeroDivisionError. 'raise'
has two advantages. One, it shortcuts the call stack so you don't have
to return some dummy value, or define another exception just to catch
it later. Two, 'raise' is a Python keyword so it's syntax-highlighted
and users should be expecting it.

abort() and redirect() are not necessarily the most intuitive but I
can't think of any better API for them. I did use them a lot in Pylons
and I miss them a bit in Pyramid.  They do have the disadvantage that
they look like normal returning function calls instead of having that
'raise' keyword. 'redirect' is particularly useful because it's not
intuitive that 'HTTPFound' means "I'm doing a redirect". If you see it
often you get used to it, but otherwise it's like, "Oh, nice, it found
something. That doesn't tell me what it's going to do with it." I
think that's a shortcoming of the HTTP status: it should have been
called 'Redirect' rather than 'Found'.

BTW, I mentioned a few days ago that my application is displaying a
blank page when I return HTTPNotFound or HTTPBadRequest, so something
is missing somewhere. The HTTP status is right but the body is empty,
no "Not Found" or anything.

-- 
Mike Orr <sluggos...@gmail.com>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to