Skip to content

Commit

Permalink
Propagate exceptions when an unhandled error happens
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Jul 25, 2020
1 parent bd98213 commit a42bebe
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions discord/shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EventType:
reconnect = 1
resume = 2
identify = 3
terminate = 4

class EventItem:
__slots__ = ('type', 'shard', 'error')
Expand Down Expand Up @@ -139,6 +140,11 @@ async def worker(self):
except self._handled_exceptions as e:
await self._handle_disconnect(e)
break
except asyncio.CancelledError:
break
except Exception as e:
self._queue.put_nowait(EventItem(EventType.terminate, self, e))
break

async def reidentify(self, exc):
self._cancel_task()
Expand All @@ -151,6 +157,10 @@ async def reidentify(self, exc):
self.ws = await asyncio.wait_for(coro, timeout=60.0)
except self._handled_exceptions as e:
await self._handle_disconnect(e)
except asyncio.CancelledError:
return
except Exception as e:
self._queue.put_nowait(EventItem(EventType.terminate, self, e))
else:
self.launch()

Expand All @@ -161,6 +171,10 @@ async def reconnect(self):
self.ws = await asyncio.wait_for(coro, timeout=60.0)
except self._handled_exceptions as e:
await self._handle_disconnect(e)
except asyncio.CancelledError:
return
except Exception as e:
self._queue.put_nowait(EventItem(EventType.terminate, self, e))
else:
self.launch()

Expand Down Expand Up @@ -312,6 +326,9 @@ async def connect(self, *, reconnect=True):
await item.shard.reidentify(item.error)
elif item.type == EventType.reconnect:
await item.shard.reconnect()
elif item.type == EventType.terminate:
await self.close()
raise item.error

async def close(self):
"""|coro|
Expand Down

0 comments on commit a42bebe

Please sign in to comment.