diff --git a/src/controller/python/chip/exceptions/__init__.py b/src/controller/python/chip/exceptions/__init__.py index 6b1969f1efe5c4..91a453b135639d 100644 --- a/src/controller/python/chip/exceptions/__init__.py +++ b/src/controller/python/chip/exceptions/__init__.py @@ -15,6 +15,8 @@ # limitations under the License. # +from __future__ import annotations + __all__ = [ "ChipStackException", "ChipStackError", @@ -32,10 +34,22 @@ class ChipStackException(Exception): class ChipStackError(ChipStackException): - def __init__(self, err, msg=None): - self.err = err + def __init__(self, chip_error: PyChipError, msg=None): + self.chip_error = chip_error self.msg = msg if msg else "Chip Stack Error %d" % err + @classmethod + def from_chip_error(cls, chip_error: PyChipError) -> ChipStackError: + return cls(chip_error, str(chip_error)) + + @property + def chip_error(self) -> PyChipError | None: + return self.chip_error + + @property + def err(self) -> int: + return self.chip_error.code + def __str__(self): return self.msg diff --git a/src/controller/python/chip/native/__init__.py b/src/controller/python/chip/native/__init__.py index 26295c2011a26b..9ee94c61ddaa7f 100644 --- a/src/controller/python/chip/native/__init__.py +++ b/src/controller/python/chip/native/__init__.py @@ -116,7 +116,7 @@ def sdk_code(self) -> int: def to_exception(self) -> typing.Union[None, chip.exceptions.ChipStackError]: if not self.is_success: - return chip.exceptions.ChipStackError(self.code, str(self)) + return chip.exceptions.ChipStackError.from_chip_error(self) def __str__(self): buf = ctypes.create_string_buffer(256)