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

It's impossible to throw web_exceptions.* from router's resolve() #1416

Closed
ikalnytskyi opened this issue Nov 21, 2016 · 2 comments · Fixed by #1460
Closed

It's impossible to throw web_exceptions.* from router's resolve() #1416

ikalnytskyi opened this issue Nov 21, 2016 · 2 comments · Fixed by #1460
Assignees
Labels

Comments

@ikalnytskyi
Copy link

Long story short

Sometimes it's very useful to have a custom router instead of built-in one. For instance, as it was mentioned in #1373, in order to implement API versioning we may want to check that API version is passed by client. If not, we need to reject the request with some error response (e.g. 412 Precondition Failed).

Unfortunately, since 1.1 it's no longer possible as any attempt to do so fails with 500 Internal Server Error on this line (match_info is None in this case):

https://github.com/KeepSafe/aiohttp/blob/v1.1.5/aiohttp/web_reqrep.py#L736

Expected behaviour

web_exceptions.* can be thrown from router's resolve() method and are transformed to appropriate HTTP response code.

Actual behaviour

Any attempt to throw web_exceptions.* lead to the 500 Internal Server Error.

Steps to reproduce

Here's an exploit (run with pytest):

import asyncio

import aiohttp.abc
import aiohttp.web


class MyRouter(aiohttp.abc.AbstractRouter):
    async def resolve(self, request):
        raise aiohttp.web.HTTPPreconditionFailed()


def test_412_is_returned():
    loop = asyncio.new_event_loop()
    app = aiohttp.web.Application(router=MyRouter(), loop=loop)
    handler = app.make_handler()

    async def test():
        async def get(url):
            server = await loop.create_server(handler, '127.0.0.1', 0)
            address, port = server.sockets[0].getsockname()

            with aiohttp.ClientSession(loop=loop) as session:
                response = await session.get(
                    'http://%s:%s%s' % (address, port, url)
                )

            await handler.finish_connections()
            server.close()

            return response

        assert (await get('/')).status == 412

    loop.run_until_complete(test())

    loop.stop()
    loop.run_forever()
    loop.close()

Your environment

  • aiohttp 1.1.5
  • python3.5
  • macos sierra
@asvetlov
Copy link
Member

I'll take a look

@asvetlov asvetlov self-assigned this Nov 22, 2016
ikalnytskyi pushed a commit to xsnippet/xsnippet-api that referenced this issue Dec 7, 2016
Since aiohttp 1.1 some backward incompatible changes have been
implemented. Despite each of them has its own issue on GitHub:

  - aio-libs/aiohttp#1373
  - aio-libs/aiohttp#1416

it's not clear when they become resolved. In order to be able to
fly on latest aiohttp version, this commit resolves those issues
by implementing workarounds on our side.
asvetlov added a commit that referenced this issue Dec 7, 2016
@asvetlov asvetlov mentioned this issue Dec 7, 2016
asvetlov added a commit that referenced this issue Dec 7, 2016
* Fix #1416

* Update CHANGES
@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants