Skip to content

Commit 893e9d6

Browse files
authored
fix: Fix workflow Exception with debug_url (#242)
1 parent c1c01c6 commit 893e9d6

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

cozepy/exception.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ 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__(
15+
self, code: Optional[int] = None, msg: str = "", logid: Optional[str] = None, debug_url: Optional[str] = None
16+
):
1517
self.code = code
1618
self.msg = msg
1719
self.logid = logid
20+
self.debug_url = debug_url
1821
if code and code > 0:
19-
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}")
22+
if self.debug_url:
23+
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}, debug_url: {self.debug_url}")
24+
else:
25+
super().__init__(f"code: {code}, msg: {msg}, logid: {logid}")
2026
else:
21-
super().__init__(f"msg: {msg}, logid: {logid}")
27+
if self.debug_url:
28+
super().__init__(f"msg: {msg}, logid: {logid}, debug_url: {self.debug_url}")
29+
else:
30+
super().__init__(f"msg: {msg}, logid: {logid}")
2231

2332

2433
class CozePKCEAuthErrorType(str, Enum):

cozepy/request.py

Lines changed: 14 additions & 12 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, debug_url, data = 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, Optional[str], Any]:
506506
try:
507507
response.read()
508508
body = response.json()
@@ -514,18 +514,19 @@ def _parse_requests_code_msg(
514514
response.text,
515515
response.headers.get("x-tt-logid"),
516516
) from e
517-
517+
debug_url = body.get("debug_url")
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"], debug_url, body.get(data_field)
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"], debug_url, None
522522
if "error_message" in body and body["error_message"] != "":
523-
return None, body["error_message"], None
523+
return None, body["error_message"], debug_url, None
524524
if data_field in body or "debug_url" in body:
525525
if "first_id" in body:
526526
return (
527527
0,
528528
"",
529+
debug_url,
529530
{
530531
"first_id": body["first_id"],
531532
"has_more": body["has_more"],
@@ -537,13 +538,14 @@ def _parse_requests_code_msg(
537538
return (
538539
0,
539540
"",
541+
debug_url,
540542
{
541543
"data": body.get(data_field),
542-
"debug_url": body.get("debug_url") or "",
544+
"debug_url": debug_url or "",
543545
"execute_id": body.get("execute_id") or None,
544546
},
545547
)
546-
return 0, "", body[data_field]
548+
return 0, "", debug_url, body[data_field]
547549
if data_field == "data.data":
548-
return 0, "", body["data"]["data"]
549-
return 0, "", body
550+
return 0, "", debug_url, body["data"]["data"]
551+
return 0, "", debug_url, body

cozepy/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from functools import lru_cache
55

6-
VERSION = "0.16.0"
6+
VERSION = "0.16.1"
77

88

99
def get_os_version() -> str:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cozepy"
3-
version = "0.16.0"
3+
version = "0.16.1"
44
description = "OpenAPI SDK for Coze(coze.com/coze.cn)"
55
authors = ["chyroc <chyroc@bytedance.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)