Skip to content

Commit 00b882f

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 d21cce0 commit 00b882f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

+22-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,32 @@
2628
"UnknownCommand",
2729
]
2830

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

3037
class ChipStackException(Exception):
3138
pass
3239

3340

3441
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
42+
def __init__(self, chip_error: PyChipError, msg=None):
43+
self.chip_error = chip_error
44+
self.msg = msg if msg else "Chip Stack Error %d" % chip_error.code
45+
46+
@classmethod
47+
def from_chip_error(cls, chip_error: PyChipError) -> ChipStackError:
48+
return cls(chip_error, str(chip_error))
49+
50+
@property
51+
def chip_error(self) -> PyChipError | None:
52+
return self.chip_error
53+
54+
@property
55+
def err(self) -> int:
56+
return self.chip_error.code
3857

3958
def __str__(self):
4059
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)