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

Add deprecation warning when HTTPException is returned instead of raised #2515

Merged
merged 2 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2415.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add DeprecationWarning for returning HTTPException
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Nicolas Braem
Nikolay Novik
Olaf Conradi
Pahaz Blinov
Panagiotis Kolokotronis
Pankaj Pandey
Pau Freixes
Paul Colomiets
Expand Down
9 changes: 9 additions & 0 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import http.server
import socket
import traceback
import warnings
from collections import deque
from contextlib import suppress
from html import escape as html_escape
Expand Down Expand Up @@ -427,6 +428,14 @@ async def start(self, message, payload, handler):
if self.access_log:
self.log_access(request, resp, loop.time() - now)

# Deprication warning (See #2415)
if isinstance(resp, HTTPException):
warnings.warn(
"returning HTTPException object is deprecated (#2415) "
"and will be removed, "
"please raise the exception instead",
DeprecationWarning)

# check payload
if not payload.is_eof():
lingering_time = self._lingering_time
Expand Down
5 changes: 5 additions & 0 deletions docs/web_quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ The exceptions are also a subclass of :class:`Response`, allowing you to either
``raise`` or ``return`` them in a
:ref:`request handler <aiohttp-web-handler>` for the same effect.

.. warning::

Returning :class:`~HTTPException` or its subclasses is deprecated and will
be removed in subsequent aiohttp versions.

The following snippets are the same::

async def handler(request):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,16 @@ async def handler(request):
async with aiohttp.ClientSession(loop=loop) as session:
async with session.post(server.make_url('/'), data=data) as resp:
assert resp.status == 200


async def test_return_http_exception_deprecated(loop, test_client):

async def handler(request):
return web.HTTPForbidden()

app = web.Application()
app.router.add_route('GET', '/', handler)
client = await test_client(app)

with pytest.warns(DeprecationWarning):
await client.get('/')