Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all construct parse errors the same #142

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
)
elif not isinstance(cmd, EnumIntegerString):
logger.debug(f"Unknown command {cmd}")
except ChecksumError:
except ConstructError as e:

Check warning on line 213 in nibe/connection/nibegw.py

View check run for this annotation

Codecov / codecov/patch

nibe/connection/nibegw.py#L213

Added line #L213 was not covered by tests
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:

Check warning on line 219 in nibe/connection/nibegw.py

View check run for this annotation

Codecov / codecov/patch

nibe/connection/nibegw.py#L219

Added line #L219 was not covered by tests
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 (

Check warning on line 12 in nibe/console_scripts/cli.py

View check run for this annotation

Codecov / codecov/patch

nibe/console_scripts/cli.py#L12

Added line #L12 was not covered by tests
Const,
ConstructError,
GreedyRange,
Int8ul,
RawCopy,
Select,
Struct,
Terminated,
)

from ..coil import CoilData
from ..connection import Connection
Expand Down Expand Up @@ -191,8 +200,12 @@
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)

Check warning on line 208 in nibe/console_scripts/cli.py

View check run for this annotation

Codecov / codecov/patch

nibe/console_scripts/cli.py#L204-L208

Added lines #L204 - L208 were not covered by tests


def read_bytes_socat(file: IO):
Expand Down