Skip to content

Commit 2d96503

Browse files
committed
Merge pull request #236 from KeepSafe/connection_error
Convert ConnectionError to ClientDisconnectedError of ServerDisconnectedError
2 parents 50813a9 + ee945aa commit 2d96503

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

aiohttp/parsers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def exception(self):
120120
return self._exception
121121

122122
def set_exception(self, exc):
123+
if isinstance(exc, ConnectionError):
124+
exc, old_exc = self._eof_exc_class(), exc
125+
exc.__cause__ = old_exc
126+
exc.__context__ = old_exc
127+
123128
self._exception = exc
124129

125130
if self._output is not None:

aiohttp/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def start(self):
239239
isinstance(handler, asyncio.Future)):
240240
yield from handler
241241

242-
except (ConnectionError, asyncio.CancelledError,
242+
except (asyncio.CancelledError,
243243
errors.ClientDisconnectedError):
244244
self.log_debug('Ignored premature client disconnection.')
245245
break

tests/test_parsers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def test_exception(self):
4848
stream.set_exception(exc)
4949
self.assertIs(stream.exception(), exc)
5050

51+
def test_exception_connection_error(self):
52+
stream = parsers.StreamParser()
53+
self.assertIsNone(stream.exception())
54+
55+
exc = ConnectionError()
56+
stream.set_exception(exc)
57+
self.assertIsNot(stream.exception(), exc)
58+
self.assertIsInstance(stream.exception(), RuntimeError)
59+
self.assertIs(stream.exception().__cause__, exc)
60+
self.assertIs(stream.exception().__context__, exc)
61+
5162
def test_exception_waiter(self):
5263
stream = parsers.StreamParser()
5364

0 commit comments

Comments
 (0)