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

Sanic exceptions #914

Closed
NikosVlagoidis opened this issue Aug 24, 2017 · 2 comments
Closed

Sanic exceptions #914

NikosVlagoidis opened this issue Aug 24, 2017 · 2 comments

Comments

@NikosVlagoidis
Copy link

How does sanic exceptions are supposed to work? The docs states that

Exceptions can be thrown from within request handlers and will automatically be handled by Sanic. Exceptions take a message as their first argument, and can also take a status code to be passed back in the HTTP response.

This is my route

@app.route("/")
async def test(request):
    abort(401)

When I make a request on the path I get a response of :

Internal Server Error
The server encountered an internal error and cannot complete your request.

2017-08-24 10:18:43 - (sanic)[ERROR]: Traceback (most recent call last): File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/app.py", line 503, in handle_request response = await response File "/home/nikos/Desktop/Side Projects/micro/test2.py", line 15, in test abort(401) File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/exceptions.py", line 262, in abort raise sanic_exception(message=message, status_code=status_code) TypeError: __init__() missing 1 required positional argument: 'scheme'

Also after a bit the connection times out and the log trace is

2017-08-24 10:18:43 - (network)[INFO][127.0.0.1:34734]: GET http://0.0.0.0:8001/ 500 144 2017-08-24 10:19:43 - (sanic)[ERROR]: Traceback (most recent call last): File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/server.py", line 143, in connection_timeout raise RequestTimeout('Request Timeout') sanic.exceptions.RequestTimeout: Request Timeout

@CharAct3
Copy link

CharAct3 commented Aug 24, 2017

There might be a bug with abort(401).

abort function raises an exception based on SanicException.

abort(401) will raise Unauthorized exception.

And Unauthorized.__init__ requires an argument scheme, but SanicException doesn't.

Maybe you can try:

@app.route("/")
async def test(request):
    raise Unauthorized("Unauthorized", "Basic")

or

@app.route("/")
async def test(request):
    abort(400)

@NikosVlagoidis
Copy link
Author

Actually, I was just testing it. Yes, the Unauthorized exception needs a scheme argument .From source code:

    def __init__(self, message, scheme, **kwargs):
        super().__init__(message)

        values = ["{!s}={!r}".format(k, v) for k, v in kwargs.items()]
        challenge = ', '.join(values)

        self.headers = {
            "WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
        }

This might be a solution?

    def __init__(self, message, scheme=None, **kwargs):

@r0fls r0fls closed this as completed in 6038813 Aug 24, 2017
r0fls added a commit that referenced this issue Aug 24, 2017
fix #914, change arguments of Unauthorized.__init__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants