Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global hooks don't play well with exceptions #246

Closed
hanjianwei opened this issue Mar 27, 2014 · 2 comments
Closed

Global hooks don't play well with exceptions #246

hanjianwei opened this issue Mar 27, 2014 · 2 comments
Labels
Milestone

Comments

@hanjianwei
Copy link

I'm writing a simple RESTful server using falcon. To support CORS I write a global after hook who adds extra response headers. But I found the hook does not work with exceptions. This is the code:

import falcon

def crossdomain(req, resp):
    resp.set_header('Access-Control-Allow-Origin', '*')

def check_auth(req, resp, params):
    if not req.auth:
        raise falcon.HTTPUnauthorized('Unauthorized', 'You should login first.')


class Todo(object):
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = '{"message": "welcome"}'

api = application = falcon.API(before=[check_auth], after=[crossdomain])

todo = Todo()
api.add_route('/todo', todo)

If authorization information is provided everything is ok:

~ ❯❯❯ http --auth falcon:falcon get http://localhost:8000/todo
HTTP/1.1 200 OK
Connection: close
Date: Thu, 27 Mar 2014 07:57:38 GMT
Server: gunicorn/18.0
access-control-allow-origin: *
content-length: 22
content-type: application/json; charset=utf-8

{
    "message": "welcome"
}

But If no authorization provided, an exception should be raised. And the extra header is not added:

~ ❯❯❯ http get http://localhost:8000/todo
HTTP/1.1 401 Unauthorized
Connection: close
Date: Thu, 27 Mar 2014 07:57:17 GMT
Server: gunicorn/18.0
content-length: 77
content-type: application/json; charset=utf-8

{
    "description": "You should login first.",
    "title": "Unauthorized"
}

wsgi middleware should be a quick solution. But I wonder is there a falcon way to do that?

Thanks!

@kgriffs kgriffs added this to the 0.2 milestone Jul 28, 2014
@kgriffs kgriffs added the bug label Jul 28, 2014
@kgriffs
Copy link
Member

kgriffs commented Jul 28, 2014

This should be fixed as of ff35bf3. Can you verify? Thanks!

@hanjianwei
Copy link
Author

Yes, it works!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants