Skip to content

Commit

Permalink
Return better error messages for client connection errors (Fixes #243)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 20, 2021
1 parent 3c8fdfe commit 3daa02e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/engineio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ async def _connect_polling(self, url, headers, engineio_path):
r = await self._send_request(
'GET', self.base_url + self._get_url_timestamp(), headers=headers,
timeout=self.request_timeout)
if r is None:
if r is None or isinstance(r, str):
self._reset()
raise exceptions.ConnectionError(
'Connection refused by the server')
r or 'Connection refused by the server')
if r.status < 200 or r.status >= 300:
self._reset()
try:
Expand Down Expand Up @@ -423,6 +423,7 @@ async def _send_request(
except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
self.logger.info('HTTP %s request to %s failed with error %s.',
method, url, exc)
return str(exc)

async def _trigger_event(self, event, *args, **kwargs):
"""Invoke an event handler."""
Expand Down Expand Up @@ -469,9 +470,9 @@ async def _read_loop_polling(self):
r = await self._send_request(
'GET', self.base_url + self._get_url_timestamp(),
timeout=max(self.ping_interval, self.ping_timeout) + 5)
if r is None:
if r is None or isinstance(r, str):
self.logger.warning(
'Connection refused by the server, aborting')
r or 'Connection refused by the server, aborting')
await self.queue.put(None)
break
if r.status < 200 or r.status >= 300:
Expand Down Expand Up @@ -589,9 +590,9 @@ async def _write_loop(self):
timeout=self.request_timeout)
for pkt in packets:
self.queue.task_done()
if r is None:
if r is None or isinstance(r, str):
self.logger.warning(
'Connection refused by the server, aborting')
r or 'Connection refused by the server, aborting')
break
if r.status < 200 or r.status >= 300:
self.logger.warning('Unexpected status code %s in server '
Expand Down
13 changes: 7 additions & 6 deletions src/engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ def _connect_polling(self, url, headers, engineio_path):
r = self._send_request(
'GET', self.base_url + self._get_url_timestamp(), headers=headers,
timeout=self.request_timeout)
if r is None:
if r is None or isinstance(r, str):
self._reset()
raise exceptions.ConnectionError(
'Connection refused by the server')
r or 'Connection refused by the server')
if r.status_code < 200 or r.status_code >= 300:
self._reset()
try:
Expand Down Expand Up @@ -525,6 +525,7 @@ def _send_request(
except requests.exceptions.RequestException as exc:
self.logger.info('HTTP %s request to %s failed with error %s.',
method, url, exc)
return str(exc)

def _trigger_event(self, event, *args, **kwargs):
"""Invoke an event handler."""
Expand Down Expand Up @@ -571,9 +572,9 @@ def _read_loop_polling(self):
r = self._send_request(
'GET', self.base_url + self._get_url_timestamp(),
timeout=max(self.ping_interval, self.ping_timeout) + 5)
if r is None:
if r is None or isinstance(r, str):
self.logger.warning(
'Connection refused by the server, aborting')
r or 'Connection refused by the server, aborting')
self.queue.put(None)
break
if r.status_code < 200 or r.status_code >= 300:
Expand Down Expand Up @@ -683,9 +684,9 @@ def _write_loop(self):
timeout=self.request_timeout)
for pkt in packets:
self.queue.task_done()
if r is None:
if r is None or isinstance(r, str):
self.logger.warning(
'Connection refused by the server, aborting')
r or 'Connection refused by the server, aborting')
break
if r.status_code < 200 or r.status_code >= 300:
self.logger.warning('Unexpected status code %s in server '
Expand Down

0 comments on commit 3daa02e

Please sign in to comment.