Skip to content

Commit

Permalink
Handle all construct parse errors the same (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 authored Jan 1, 2024
2 parents 756a793 + 7c3322e commit 4b109e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
11 changes: 5 additions & 6 deletions nibe/connection/nibegw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
BitStruct,
Bytes,
Checksum,
ChecksumError,
Const,
ConstructError,
Container,
Enum,
EnumIntegerString,
Expand Down Expand Up @@ -210,16 +210,15 @@ def datagram_received(self, data: bytes, addr):
)
elif not isinstance(cmd, EnumIntegerString):
logger.debug(f"Unknown command {cmd}")
except ChecksumError:
except ConstructError as e:
logger.warning(
f"Ignoring packet from {addr} due to checksum error: {hexlify(data).decode('utf-8')}"
f"Ignoring packet from {addr} due to parse error: {hexlify(data).decode('utf-8')}: {e}"
)
except NibeException as e:
logger.error(f"Failed handling packet from {addr}: {e}")
except Exception as e:
except Exception:
logger.exception(
f"Unexpected exception during parsing packet data '{hexlify(data).decode('utf-8')}' from {addr}",
e,
f"Unexpected exception during parsing packet data '{hexlify(data).decode('utf-8')}' from {addr}"
)

async def read_product_info(
Expand Down
19 changes: 16 additions & 3 deletions nibe/console_scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
from typing import IO

import asyncclick as click
from construct import Const, GreedyRange, Int8ul, RawCopy, Select, Struct, Terminated
from construct import (
Const,
ConstructError,
GreedyRange,
Int8ul,
RawCopy,
Select,
Struct,
Terminated,
)

from ..coil import CoilData
from ..connection import Connection
Expand Down Expand Up @@ -191,8 +200,12 @@ async def parse_data(data: str, type: str):
raw = bytes.fromhex(data)
elif type == "bytes":
raw = bytes(literal_eval(data))
request = Block.parse(raw)
click.echo(request)

try:
request = Block.parse(raw)
click.echo(request)
except ConstructError as exception:
click.echo(f"Failed to parse: {exception}", err=True)


def read_bytes_socat(file: IO):
Expand Down

0 comments on commit 4b109e4

Please sign in to comment.