From bde782886539288a2804962666252c60fc0ae45d Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sun, 1 Sep 2024 09:47:50 -0400 Subject: [PATCH] doorcontrol: Show hex value in hid.Credential exceptions --- doorcontrol/hid/Credential.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doorcontrol/hid/Credential.py b/doorcontrol/hid/Credential.py index 333c479..5bc3248 100644 --- a/doorcontrol/hid/Credential.py +++ b/doorcontrol/hid/Credential.py @@ -12,13 +12,13 @@ class InvalidHexCode(Exception): class Not26Bit(InvalidHexCode): - def __init__(self) -> None: - super().__init__("Card number > 26 bits") + def __init__(self, bits: bitstring.Bits) -> None: + super().__init__(f"Card number > 26 bits [{bits.hex}]") class InvalidParity(InvalidHexCode): - def __init__(self, even_odd: Literal["even", "odd"]) -> None: - super().__init__(f"Bad {even_odd} parity") + def __init__(self, bits: bitstring.Bits, even_odd: Literal["even", "odd"]) -> None: + super().__init__(f"Bad {even_odd} parity [{bits.hex}]") @dataclasses.dataclass(frozen=True) @@ -41,11 +41,11 @@ class Credential: bits = bitstring.Bits(hex=hex_code) if bits[:6].any(1): - raise Not26Bit + raise Not26Bit(bits) if bits[6] != bits[7:19].count(1) % 2: - raise InvalidParity("even") + raise InvalidParity(bits, "even") if bits[31] != (bits[19:31].count(0) % 2): - raise InvalidParity("odd") + raise InvalidParity(bits, "odd") return cls(bits)