Skip to content

Commit

Permalink
Fix api error (#123)
Browse files Browse the repository at this point in the history
add req_id in error
  • Loading branch information
danielhjz authored Dec 7, 2023
1 parent 7198260 commit 6e3d98c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cookbook/agents/langchain_agent_with_qianfan_llm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.5"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
8 changes: 6 additions & 2 deletions src/qianfan/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""the collection of errors for this library
"""

from typing import Any


class QianfanError(Exception):
"""Base exception class for the qianfan sdk."""
Expand All @@ -31,14 +33,16 @@ class NotImplmentError(QianfanError):
class APIError(QianfanError):
"""Base exception clas for the qianfan api error"""

def __init__(self, error_code: int, error_msg: str) -> None:
def __init__(self, error_code: int, error_msg: str, req_id: Any) -> None:
"""
init with error code and error message from api response
"""
self.error_code = error_code
self.error_msg = error_msg
self.req_id = req_id
super().__init__(
f"api return error, code: {self.error_code }, msg: {self.error_msg}"
f"api return error, req_id: {self.req_id} code: {self.error_code }, msg:"
f" {self.error_msg}"
)


Expand Down
3 changes: 2 additions & 1 deletion src/qianfan/resources/requestor/console_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ def _check_error(self, body: Dict[str, Any]) -> None:
check whether error_code is in the response body
"""
if "error_code" in body:
req_id = body.get("log_id", "")
error_code = body["error_code"]
err_msg = body.get("error_msg", "no error message found in response body")
log_error(
f"console api request failed with error code: {error_code}, err msg:"
f" {err_msg}, please check the api doc"
)
raise errors.APIError(error_code, err_msg)
raise errors.APIError(error_code, err_msg, req_id)

def _request_console_api(
self, req: QfRequest, ak: str, sk: str, retry_config: RetryConfig
Expand Down
3 changes: 2 additions & 1 deletion src/qianfan/resources/requestor/openapi_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def _check_error(self, body: Dict[str, Any]) -> None:
raise AccessTokenExpiredError
"""
if "error_code" in body:
req_id = body.get("id", "")
error_code = body["error_code"]
err_msg = body.get("error_msg", "no error message found in response body")
log_error(
Expand All @@ -115,7 +116,7 @@ def _check_error(self, body: Dict[str, Any]) -> None:
APIErrorCode.APITokenInvalid.value,
}:
raise errors.AccessTokenExpiredError
raise errors.APIError(error_code, err_msg)
raise errors.APIError(error_code, err_msg, req_id)

def llm(
self,
Expand Down
29 changes: 14 additions & 15 deletions src/qianfan/trainer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from qianfan.errors import InternalError, InvalidArgumentError
from qianfan.trainer.consts import ActionState
from qianfan.trainer.event import Event, EventHandler, dispatch_event
from qianfan.utils import log_debug, utils
from qianfan.utils import log_debug, log_error, utils

Input = TypeVar("Input")
Output = TypeVar("Output")
Expand Down Expand Up @@ -216,20 +216,19 @@ def with_event(func: Callable[..., Any]) -> Callable[..., Any]:

def wrapper(self: BaseAction, **kwargs: Any) -> Any:
"""
mehtod wrapper
"""
# try:
log_debug(f"action[{self.__class__.__name__}][{self.id}] Preceding")
self.action_event(ActionState.Preceding, "", {})
resp = func(self, **kwargs)
self.action_event(ActionState.Done, "", resp)
log_debug(f"action[{self.__class__.__name__}][{self.id}] Done")
return resp
# except Exception as e:
# log_error(f"action[{self.__class__.__name__}][{self.id}] error {e}")
# self.action_error_event(e)

# return {"error": e}
method wrapper
"""
try:
log_debug(f"action[{self.__class__.__name__}][{self.id}] Preceding")
self.action_event(ActionState.Preceding, "", {})
resp = func(self, **kwargs)
self.action_event(ActionState.Done, "", resp)
log_debug(f"action[{self.__class__.__name__}][{self.id}] Done")
return resp
except Exception as e:
log_error(f"action[{self.__class__.__name__}][{self.id}] error {e}")
self.action_error_event(e)
return {"error": e}

return wrapper

Expand Down

0 comments on commit 6e3d98c

Please sign in to comment.