Skip to content

Commit 66ad50d

Browse files
committed
[Python] Store original PyChipError in ChipStackException
This change stores the original PyChipError in ChipStackException so that details of the original error code can still be retrieved. This is interesting to use the properties returning processed information about the original error code. It also preserves the line and code file which can be helpful.
1 parent b3548dc commit 66ad50d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/controller/python/chip/exceptions/__init__.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616
#
1717

18+
from __future__ import annotations
19+
1820
__all__ = [
1921
"ChipStackException",
2022
"ChipStackError",
@@ -26,15 +28,31 @@
2628
"UnknownCommand",
2729
]
2830

31+
from typing import TYPE_CHECKING
32+
33+
if TYPE_CHECKING:
34+
from chip.native import PyChipError
2935

3036
class ChipStackException(Exception):
3137
pass
3238

3339

3440
class ChipStackError(ChipStackException):
35-
def __init__(self, err, msg=None):
36-
self.err = err
37-
self.msg = msg if msg else "Chip Stack Error %d" % err
41+
def __init__(self, chip_error: PyChipError, msg=None):
42+
self.chip_error = chip_error
43+
self.msg = msg if msg else "Chip Stack Error %d" % chip_error.code
44+
45+
@classmethod
46+
def from_chip_error(cls, chip_error: PyChipError) -> ChipStackError:
47+
return cls(chip_error, str(chip_error))
48+
49+
@property
50+
def chip_error(self) -> PyChipError | None:
51+
return self.chip_error
52+
53+
@property
54+
def err(self) -> int:
55+
return self.chip_error.code
3856

3957
def __str__(self):
4058
return self.msg

src/controller/python/chip/native/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def sdk_code(self) -> int:
116116

117117
def to_exception(self) -> typing.Union[None, chip.exceptions.ChipStackError]:
118118
if not self.is_success:
119-
return chip.exceptions.ChipStackError(self.code, str(self))
119+
return chip.exceptions.ChipStackError.from_chip_error(self)
120120

121121
def __str__(self):
122122
buf = ctypes.create_string_buffer(256)

0 commit comments

Comments
 (0)