Skip to content

Commit c8d4a05

Browse files
author
zhangzhiwen
committed
feat: error append debug_url
1 parent c1c01c6 commit c8d4a05

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

cozepy/exception.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ class CozeError(Exception):
1111

1212

1313
class CozeAPIError(CozeError):
14-
def __init__(self, code: Optional[int] = None, msg: str = "", logid: Optional[str] = None):
14+
def __init__(self, code: Optional[int] = None, msg: str = "", logid: Optional[str] = None,
15+
debug_url: Optional[str] = None):
1516
self.code = code
1617
self.msg = msg
1718
self.logid = logid
19+
self.debug_url = debug_url
1820
if code and code > 0:
19-
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}")
21+
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}, debug_url: {debug_url}")
2022
else:
21-
super().__init__(f"msg: {msg}, logid: {logid}")
23+
super().__init__(f"msg: {msg}, logid: {logid}, debug_url: {debug_url}")
2224

2325

2426
class CozePKCEAuthErrorType(str, Enum):

cozepy/request.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,16 @@ def _parse_response(
475475
if resp_content_type and "audio" in resp_content_type:
476476
return FileHTTPResponse(response) # type: ignore
477477

478-
code, msg, data = self._parse_requests_code_msg(method, url, response, data_field)
478+
code, msg, data, debug_url = self._parse_requests_code_msg(method, url, response, data_field)
479479

480480
if code is not None and code > 0:
481481
log_warning("request %s#%s failed, logid=%s, code=%s, msg=%s", method, url, logid, code, msg)
482-
raise CozeAPIError(code, msg, logid)
482+
raise CozeAPIError(code, msg, logid, debug_url)
483483
elif code is None and msg != "":
484484
log_warning("request %s#%s failed, logid=%s, msg=%s", method, url, logid, msg)
485485
if msg in COZE_PKCE_AUTH_ERROR_TYPE_ENUMS:
486486
raise CozePKCEAuthError(CozePKCEAuthErrorType(msg), logid)
487-
raise CozeAPIError(code, msg, logid)
487+
raise CozeAPIError(code, msg, logid, debug_url)
488488
if isinstance(cast, List):
489489
item_cast = cast[0]
490490
return [item_cast.model_validate(item) for item in data]
@@ -502,7 +502,7 @@ def _parse_response(
502502

503503
def _parse_requests_code_msg(
504504
self, method: str, url: str, response: Response, data_field: str = "data"
505-
) -> Tuple[Optional[int], str, Any]:
505+
) -> Tuple[Optional[int], str, Any, Optional[str]]:
506506
try:
507507
response.read()
508508
body = response.json()
@@ -516,11 +516,11 @@ def _parse_requests_code_msg(
516516
) from e
517517

518518
if "code" in body and "msg" in body and int(body["code"]) > 0:
519-
return int(body["code"]), body["msg"], body.get(data_field)
519+
return int(body["code"]), body["msg"], body.get(data_field), body.get("debug_url")
520520
if "error_code" in body and body["error_code"] in COZE_PKCE_AUTH_ERROR_TYPE_ENUMS:
521-
return None, body["error_code"], None
521+
return None, body["error_code"], None, body.get("debug_url")
522522
if "error_message" in body and body["error_message"] != "":
523-
return None, body["error_message"], None
523+
return None, body["error_message"], None, body.get("debug_url")
524524
if data_field in body or "debug_url" in body:
525525
if "first_id" in body:
526526
return (
@@ -532,6 +532,7 @@ def _parse_requests_code_msg(
532532
"last_id": body["last_id"],
533533
"items": body["data"],
534534
},
535+
body.get("debug_url")
535536
)
536537
if "debug_url" in body:
537538
return (
@@ -542,8 +543,9 @@ def _parse_requests_code_msg(
542543
"debug_url": body.get("debug_url") or "",
543544
"execute_id": body.get("execute_id") or None,
544545
},
546+
body.get("debug_url")
545547
)
546-
return 0, "", body[data_field]
548+
return 0, "", body[data_field], body.get("debug_url")
547549
if data_field == "data.data":
548-
return 0, "", body["data"]["data"]
549-
return 0, "", body
550+
return 0, "", body["data"]["data"], body.get("debug_url")
551+
return 0, "", body, body.get("debug_url")

0 commit comments

Comments
 (0)