Skip to content

Commit e91af72

Browse files
jcjfasvetlov
authored andcommitted
Make HTTPException truthy (#3098)
1 parent 319d07f commit e91af72

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

CHANGES/3096.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve the cause when `HTTPException` is raised from another exception.

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Julien Duponchelle
112112
Jungkook Park
113113
Junjie Tao
114114
Justas Trimailovas
115+
Justin Foo
115116
Justin Turner Arthur
116117
Kay Zheng
117118
Kimmo Parviainen-Jalanko

aiohttp/web_exceptions.py

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def __init__(self, *, headers=None, reason=None,
8383
if self.body is None and not self.empty_body:
8484
self.text = "{}: {}".format(self.status, self.reason)
8585

86+
def __bool__(self):
87+
return True
88+
8689

8790
class HTTPError(HTTPException):
8891
"""Base class for exceptions with status codes in the 400s and 500s."""

tests/test_web_exceptions.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import re
3+
from traceback import format_exception
34
from unittest import mock
45

56
import pytest
@@ -172,3 +173,14 @@ def test_link_header_451(buf, request):
172173

173174
assert 'http://warning.or.kr/' == resp.link
174175
assert '<http://warning.or.kr/>; rel="blocked-by"' == resp.headers['Link']
176+
177+
178+
def test_HTTPException_retains_cause():
179+
with pytest.raises(web.HTTPException) as ei:
180+
try:
181+
raise Exception('CustomException')
182+
except Exception as exc:
183+
raise web.HTTPException() from exc
184+
tb = ''.join(format_exception(ei.type, ei.value, ei.tb))
185+
assert 'CustomException' in tb
186+
assert 'direct cause' in tb

0 commit comments

Comments
 (0)