Skip to content

Commit

Permalink
Correct error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Drapersniper committed Mar 10, 2022
1 parent 79cac09 commit b4556df
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lavalink/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import discord
from discord.ext.commands import Bot

from . import enums, log, node, player_manager
from . import enums, log, node, player_manager, errors

__all__ = [
"initialize",
Expand Down Expand Up @@ -130,7 +130,7 @@ def get_player(guild_id: int) -> player_manager.Player:
async def _on_guild_remove(guild):
try:
p = get_player(guild.id)
except (IndexError, KeyError):
except (errors.NodeNotFound, errors.PlayerNotFound):
pass
else:
await p.disconnect()
Expand Down Expand Up @@ -185,7 +185,7 @@ def _get_event_args(data: enums.LavalinkEvents, raw_data: dict):
try:
node_ = node.get_node(guild_id, ignore_ready_status=True)
player = node_.player_manager.get_player(guild_id)
except (IndexError, KeyError):
except (errors.NodeNotFound, errors.PlayerNotFound):
if data != enums.LavalinkEvents.TRACK_END:
log.debug(
"Got an event for a guild that we have no player for."
Expand Down Expand Up @@ -264,7 +264,7 @@ def _get_update_args(data: enums.PlayerState, raw_data: dict):

try:
player = get_player(guild_id)
except (KeyError, IndexError):
except (errors.NodeNotFound, errors.PlayerNotFound):
log.debug(
"Got a player update for a guild that we have no player for."
" This may be because of a forced voice channel disconnect."
Expand Down
2 changes: 1 addition & 1 deletion lavalink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ async def on_socket_response(data):

try:
node = get_node(guild_id, ignore_ready_status=True)
except IndexError:
except NodeNotFound:
ws_discord_log.info(
f"Received unhandled Discord WS voice response for guild: %d, %s", int(guild_id), data
)
Expand Down
6 changes: 3 additions & 3 deletions lavalink/player_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from . import log, ws_rll_log
from .enums import *
from .errors import PlayerNotFound
from .errors import PlayerNotFound, NodeNotFound
from .rest_api import RESTClient, Track

if TYPE_CHECKING:
Expand Down Expand Up @@ -499,7 +499,7 @@ def _ensure_player(
if channel is not None:
try:
p = self.get_player(guild_id)
except KeyError:
except (PlayerNotFound, NodeNotFound):
log.debug("Received voice channel connection without a player.")
p = Player(self, channel)
self._player_dict[guild_id] = p
Expand All @@ -508,7 +508,7 @@ def _ensure_player(
async def _remove_player(self, guild_id: int):
try:
p = self.get_player(guild_id)
except KeyError:
except (PlayerNotFound, NodeNotFound):
pass
else:
del self._player_dict[guild_id]
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from discord.ext.commands import AutoShardedBot

import lavalink.node
from lavalink import NodeNotFound, PlayerNotFound


class ProxyWebSocket:
Expand Down Expand Up @@ -158,7 +159,7 @@ async def node(bot):

try:
await node_.disconnect()
except KeyError:
except (PlayerNotFound, NodeNotFound):
pass


Expand Down

0 comments on commit b4556df

Please sign in to comment.