Skip to content

Commit

Permalink
Refactor error handling to more correctly describe when errors occur
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasJRH committed May 27, 2024
1 parent 14d29a4 commit 008ee5f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
36 changes: 22 additions & 14 deletions modules/telemetry/telemetry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,36 @@ def parse_radio_block(pkt_version: int, block_header: BlockHeader, hex_block_con
block_bytes: bytes = bytes.fromhex(hex_block_contents)

try:
# TODO Make an interface to support multiple v1/v2/v3 objects
block_subtype = v1db.DataBlockSubtype(block_header.message_subtype)
block_contents = v1db.DataBlock.parse(block_subtype, block_bytes)
block_name = block_subtype.name.lower()

logger.debug(str(block_contents))

# TODO fix at some point
# if block == DataBlockSubtype.STATUS:
# self.status.rocket = jsp.RocketData.from_data_block(block)
# return

return ParsedBlock(block_name, block_header, dict(block_contents)) # type: ignore

except ValueError:
logger.error("Invalid data block subtype")
logger.error(f"Invalid data block subtype {block_header.message_subtype}!")
return

try:
# TODO Make an interface to support multiple v1/v2/v3 objects
block_contents = v1db.DataBlock.parse(block_subtype, block_bytes)
except NotImplementedError:
logger.warning(
f"Block parsing for type {block_header.message_type}, with subtype {block_header.message_subtype} not \
implemented!"
)
return
except v1db.DataBlockException as e:
logger.error(e)
logger.error(f"Block header: {block_header}")
logger.error(f"Block contents: {hex_block_contents}")
return

block_name = block_subtype.name.lower()

logger.debug(str(block_contents))

# TODO fix at some point
# if block == DataBlockSubtype.STATUS:
# self.status.rocket = jsp.RocketData.from_data_block(block)
# return

return ParsedBlock(block_name, block_header, dict(block_contents)) # type: ignore


def parse_rn2483_transmission(data: str, config: Config) -> Optional[ParsedTransmission]:
Expand Down
26 changes: 12 additions & 14 deletions modules/telemetry/v1/data_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@
from modules.misc.converter import metres_to_feet, milli_degrees_to_celsius, pascals_to_psi


class BlockException(Exception):
pass
class DataBlockException(Exception):
"""Exception raised when an error occurs while parsing a data block."""


class BlockUnknownException(BlockException):
pass


class DataBlockException(BlockException):
pass


class DataBlockUnknownException(BlockUnknownException):
pass
def __init__(self, subtype_name: str, error: str):
self.subtype_name = subtype_name
self.error = error
super().__init__(f"Error parsing {subtype_name} block: {error}")


class DataBlockSubtype(IntEnum):
Expand Down Expand Up @@ -119,7 +112,12 @@ def parse(block_subtype: DataBlockSubtype, payload: bytes) -> DataBlock:
if subtype is None:
raise NotImplementedError

return subtype.from_bytes(payload=payload)
try:
subtype_instance = subtype.from_bytes(payload)
except Exception as e:
raise DataBlockException(subtype.__name__, str(e))

return subtype_instance


class DebugMessageDB(DataBlock):
Expand Down

0 comments on commit 008ee5f

Please sign in to comment.