Skip to content

Commit

Permalink
Log frame errors at debug by default.
Browse files Browse the repository at this point in the history
FrameDataError is the only child of FrameError exception class that is logged as warning, so it makes sense to log all exceptions that inherit from FrameError class, excluding FrameDataError, at debug by default.
  • Loading branch information
denpamusic committed Sep 27, 2023
1 parent 827d980 commit 58b5283
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
13 changes: 5 additions & 8 deletions pyplumio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from pyplumio.const import ATTR_CONNECTED, DeviceType
from pyplumio.devices import Addressable, get_device_handler
from pyplumio.exceptions import (
ChecksumError,
FrameDataError,
FrameError,
ReadError,
UnknownDeviceError,
UnknownFrameError,
)
from pyplumio.frames.requests import StartMasterRequest
from pyplumio.helpers.event_manager import EventManager
Expand Down Expand Up @@ -87,16 +86,14 @@ async def frame_producer(
device = self.setup_device_entry(response.sender)
read_queue.put_nowait((device, response))

except UnknownFrameError as e:
_LOGGER.debug("Unknown frame type: %s", e)
except FrameDataError as e:
_LOGGER.warning("Incorrect payload: %s", e)
except ReadError as e:
_LOGGER.debug("Read error: %s", e)
except ChecksumError as e:
_LOGGER.debug("Checksum error: %s", e)
except FrameError as e:
_LOGGER.warning("Can't process received frame: %s", e)
except UnknownDeviceError as e:
_LOGGER.debug("Unknown device: %s", e)
except FrameError as e:
_LOGGER.debug("Can't process received frame: %s", e)
except (OSError, asyncio.TimeoutError):
self.create_task(self.connection_lost())
break
Expand Down
27 changes: 10 additions & 17 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from pyplumio.const import ATTR_CONNECTED, DeviceType, EncryptionType
from pyplumio.devices.ecomax import EcoMAX
from pyplumio.exceptions import (
ChecksumError,
FrameDataError,
FrameError,
ReadError,
UnknownDeviceError,
UnknownFrameError,
)
from pyplumio.frames import Response
from pyplumio.frames.requests import (
Expand Down Expand Up @@ -131,11 +130,10 @@ async def test_frame_producer(protocol: Protocol, caplog) -> None:
protocol.reader.read = AsyncMock(
side_effect=(
response,
UnknownFrameError("test unknown frame error"),
ChecksumError("test checksum error"),
FrameError("test frame error"),
UnknownDeviceError("test unknown device error"),
ReadError("test read error"),
FrameDataError("test frame data error"),
Exception("test generic error"),
ConnectionError,
)
Expand All @@ -146,7 +144,7 @@ async def test_frame_producer(protocol: Protocol, caplog) -> None:
# Create mock queues.
mock_read_queue = AsyncMock(spec=asyncio.Queue)
mock_write_queue = AsyncMock(spec=asyncio.Queue)
mock_write_queue.qsize = Mock(side_effect=(1, 0, 0, 0, 0, 0, 0, 0))
mock_write_queue.qsize = Mock(side_effect=(1, 0, 0, 0, 0, 0, 0))
mock_write_queue.get = AsyncMock(return_value="test_request")

with patch("pyplumio.devices.ecomax.EcoMAX") as mock_device, patch(
Expand All @@ -158,16 +156,6 @@ async def test_frame_producer(protocol: Protocol, caplog) -> None:
(
"pyplumio.protocol",
logging.DEBUG,
"Unknown frame type: test unknown frame error",
),
(
"pyplumio.protocol",
logging.DEBUG,
"Checksum error: test checksum error",
),
(
"pyplumio.protocol",
logging.WARNING,
"Can't process received frame: test frame error",
),
(
Expand All @@ -180,6 +168,11 @@ async def test_frame_producer(protocol: Protocol, caplog) -> None:
logging.DEBUG,
"Read error: test read error",
),
(
"pyplumio.protocol",
logging.WARNING,
"Incorrect payload: test frame data error",
),
(
"pyplumio.protocol",
logging.ERROR,
Expand All @@ -194,8 +187,8 @@ async def test_frame_producer(protocol: Protocol, caplog) -> None:
)
mock_connection_lost.assert_called_once()
assert mock_write_queue.get.await_count == 1
assert mock_write_queue.qsize.call_count == 8
assert protocol.reader.read.await_count == 8
assert mock_write_queue.qsize.call_count == 7
assert protocol.reader.read.await_count == 7


@patch("pyplumio.frames.requests.CheckDeviceRequest.response")
Expand Down

0 comments on commit 58b5283

Please sign in to comment.