Skip to content

Commit a2a072d

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 eac5a83 commit a2a072d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

+16-2
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",
@@ -32,10 +34,22 @@ class ChipStackException(Exception):
3234

3335

3436
class ChipStackError(ChipStackException):
35-
def __init__(self, err, msg=None):
36-
self.err = err
37+
def __init__(self, chip_error: PyChipError, msg=None):
38+
self.chip_error = chip_error
3739
self.msg = msg if msg else "Chip Stack Error %d" % err
3840

41+
@classmethod
42+
def from_chip_error(cls, chip_error: PyChipError) -> ChipStackError:
43+
return cls(chip_error, str(chip_error))
44+
45+
@property
46+
def chip_error(self) -> PyChipError | None:
47+
return self.chip_error
48+
49+
@property
50+
def err(self) -> int:
51+
return self.chip_error.code
52+
3953
def __str__(self):
4054
return self.msg
4155

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)