Repository: nautechsystems/nautilus_trader
File: nautilus_trader/adapters/dydx/execution.py (Python)

Code before:
        plural = "" if len_reports == 1 else "s"
        self._log.info(f"Received {len(reports)} PositionStatusReport{plural}")
        return reports

    def _handle_ws_message(self, raw: bytes) -> None:  # noqa: C901
        try:
            ws_message = self._decoder_ws_msg_general.decode(raw)
            ws_message_channel = ws_message.channel
            ws_message_type = ws_message.type

            if ws_message_type == "channel_data":
                if ws_message_channel == "v4_block_height":
                    self._handle_block_height_channel_data(raw)
                elif ws_message_channel == "v4_subaccounts":
                    self._handle_subaccounts_channel_data(raw)
                elif ws_message_channel == "v4_markets":
                    self._handle_markets(raw)
            elif ws_message_type == "subscribed":
                if ws_message_channel == "v4_block_height":
                    self._handle_block_height_subscribed(raw)
                elif ws_message_channel == "v4_subaccounts":
                    self._handle_subaccounts_subscribed(raw)
                elif ws_message_channel == "v4_markets":
                    self._handle_markets_subscribed(raw)
            elif ws_message_type == "unsubscribed":
                self._log.info(
                    f"Unsubscribed from channel {ws_message_channel} for {ws_message.id}",
                )
            elif ws_message_type == "connected":
                self._log.info("Websocket connected")
            else:
                self._log.error(f"Unknown message `{ws_message_type}`: {raw.decode()}")
        except Exception as e:
            self._log.error(f"Failed to parse websocket message: {raw.decode()} with error {e}")

    def _handle_block_height_subscribed(self, raw: bytes) -> None:
        try:
            msg: DYDXWsBlockHeightSubscribedData = self._decoder_ws_block_height_subscribed.decode(
                raw,
            )
            self._block_height = int(msg.contents.height)

        except Exception as e:
            self._log.error(
                f"Failed to parse block height subscribed message: {raw.decode()} with error {e}",
            )

    def _handle_block_height_channel_data(self, raw: bytes) -> None:
        try:
            msg: DYDXWsBlockHeightChannelData = self._decoder_ws_block_height_channel.decode(
                raw,

Review comment (suggestion):
Perhaps add an `else` block here too which logs an error when a message with an unknown channel is received.

Code after:
        plural = "" if len_reports == 1 else "s"
        self._log.info(f"Received {len(reports)} PositionStatusReport{plural}")
        return reports

    def _handle_ws_message(self, raw: bytes) -> None:  # noqa: C901
        try:
            ws_message = self._decoder_ws_msg_general.decode(raw)
            ws_message_channel = ws_message.channel
            ws_message_type = ws_message.type

            if ws_message_type == "channel_data":
                if ws_message_channel == "v4_block_height":
                    self._handle_block_height_channel_data(raw)
                elif ws_message_channel == "v4_subaccounts":
                    self._handle_subaccounts_channel_data(raw)
                elif ws_message_channel == "v4_markets":
                    self._handle_markets(raw)
                else:
                    self._log.error(f"Unknown message `{ws_message_type}`: {raw.decode()}")
            elif ws_message_type == "subscribed":
                if ws_message_channel == "v4_block_height":
                    self._handle_block_height_subscribed(raw)
                elif ws_message_channel == "v4_subaccounts":
                    self._handle_subaccounts_subscribed(raw)
                elif ws_message_channel == "v4_markets":
                    self._handle_markets_subscribed(raw)
                else:
                    self._log.error(f"Unknown message `{ws_message_type}`: {raw.decode()}")
            elif ws_message_type == "unsubscribed":
                self._log.info(
                    f"Unsubscribed from channel {ws_message_channel} for {ws_message.id}",
                )
            elif ws_message_type == "connected":
                self._log.info("Websocket connected")
            else:
                self._log.error(f"Unknown message `{ws_message_type}`: {raw.decode()}")
        except Exception as e:
            self._log.error(f"Failed to parse websocket message: {raw.decode()} with error {e}")

    def _handle_block_height_subscribed(self, raw: bytes) -> None:
        try:
            msg: DYDXWsBlockHeightSubscribedData = self._decoder_ws_block_height_subscribed.decode(
                raw,
            )
            self._block_height = int(msg.contents.height)

        except Exception as e:
            self._log.error(
                f"Failed to parse block height subscribed message: {raw.decode()} with error {e}",
            )