-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add custom exceptions and replace direct Exception usage (#19)
- Defined custom exception classes for the project - Replaced direct usage of Exception with custom exceptions - Improved error handling and code clarity with specific exception types
- Loading branch information
Showing
6 changed files
with
75 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class CozeError(Exception): | ||
""" | ||
base class for all coze errors | ||
""" | ||
|
||
pass | ||
|
||
|
||
class CozeAPIError(CozeError): | ||
""" | ||
base class for all api errors | ||
""" | ||
|
||
def __init__(self, code: int = None, msg: str = "", logid: str = None): | ||
self.code = code | ||
self.msg = msg | ||
self.logid = logid | ||
if code and code > 0: | ||
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}") | ||
else: | ||
super().__init__(f"msg: {msg}, logid: {logid}") | ||
|
||
|
||
class CozeEventError(CozeError): | ||
""" | ||
base class for all event errors | ||
""" | ||
|
||
def __init__(self, field: str = "", data: str = ""): | ||
self.field = field | ||
self.data = data | ||
if field: | ||
super().__init__(f"invalid event, field: {field}, data: {data}") | ||
else: | ||
super().__init__(f"invalid event, data: {data}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from cozepy import CozeAPIError | ||
|
||
|
||
def test_coze_api_error(): | ||
err = CozeAPIError(1, "msg", "logid") | ||
assert err.code == 1 | ||
assert err.msg == "msg" | ||
assert err.logid == "logid" | ||
assert str(err) == "code: 1, msg: msg, logid: logid" | ||
|
||
err = CozeAPIError(None, "msg", "logid") | ||
assert err.code is None | ||
assert err.msg == "msg" | ||
assert err.logid == "logid" | ||
assert str(err) == "msg: msg, logid: logid" |