From a5a4ce98ff2cec9982d80b6ce4afe3607f2dc6bb Mon Sep 17 00:00:00 2001 From: Guy Avnon Date: Mon, 2 Oct 2023 17:04:40 +0700 Subject: [PATCH] [CM-8195] now logs error if trying to log to a non llm project (#67) * now logs error if trying to log to a non llm project * fixed lint errors * added an abstract function for handling responses * added safe mechanism * fixed lint * fixes * fixed lint * lint --- src/comet_llm/backend_error_codes.py | 15 ++++++++++ .../request_exception_wrapper.py | 3 ++ src/comet_llm/handlers/failed_response.py | 28 +++++++++++++++++++ src/comet_llm/logging_messages.py | 16 +++++++++++ .../test_request_exception_wrapper.py | 24 +++++++++++++++- 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/comet_llm/backend_error_codes.py create mode 100644 src/comet_llm/handlers/failed_response.py create mode 100644 src/comet_llm/logging_messages.py diff --git a/src/comet_llm/backend_error_codes.py b/src/comet_llm/backend_error_codes.py new file mode 100644 index 0000000000..60afd65fc6 --- /dev/null +++ b/src/comet_llm/backend_error_codes.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# ******************************************************* +# ____ _ _ +# / ___|___ _ __ ___ ___| |_ _ __ ___ | | +# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | +# | |__| (_) | | | | | | __/ |_ _| | | | | | | +# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| +# +# Sign up for free at https://www.comet.com +# Copyright (C) 2015-2023 Comet ML INC +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this package. +# ******************************************************* + +UNABLE_TO_LOG_TO_NON_LLM_PROJECT = 34323 diff --git a/src/comet_llm/experiment_api/request_exception_wrapper.py b/src/comet_llm/experiment_api/request_exception_wrapper.py index 091bcb8540..d9030327a6 100644 --- a/src/comet_llm/experiment_api/request_exception_wrapper.py +++ b/src/comet_llm/experiment_api/request_exception_wrapper.py @@ -21,6 +21,7 @@ import requests # type: ignore from .. import config, exceptions +from ..handlers import failed_response LOGGER = logging.getLogger(__name__) @@ -42,6 +43,8 @@ def wrapper(*args, **kwargs) -> Any: # type: ignore f"{comet_url}. Check that your Comet " f"installation is up-to-date and check the traceback for more details." ) + if exception.response is not None: + exception_args.append(failed_response.handle(exception.response)) _debug_log(exception) diff --git a/src/comet_llm/handlers/failed_response.py b/src/comet_llm/handlers/failed_response.py new file mode 100644 index 0000000000..e71723e5b6 --- /dev/null +++ b/src/comet_llm/handlers/failed_response.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# ******************************************************* +# ____ _ _ +# / ___|___ _ __ ___ ___| |_ _ __ ___ | | +# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | +# | |__| (_) | | | | | | __/ |_ _| | | | | | | +# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| +# +# Sign up for free at https://www.comet.com +# Copyright (C) 2015-2023 Comet ML INC +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this package. +# ******************************************************* +import json +from typing import Optional + +import requests # type: ignore + +from .. import backend_error_codes, logging_messages + +SDK_ERROR_CODES_LOGGING_MESSAGE = { + backend_error_codes.UNABLE_TO_LOG_TO_NON_LLM_PROJECT: logging_messages.UNABLE_TO_LOG_TO_NON_LLM_PROJECT +} + + +def handle(response: requests.Response) -> Optional[str]: + sdk_error_code = json.loads(response.text)["sdk_error_code"] + return SDK_ERROR_CODES_LOGGING_MESSAGE.get(sdk_error_code) diff --git a/src/comet_llm/logging_messages.py b/src/comet_llm/logging_messages.py new file mode 100644 index 0000000000..58773d0547 --- /dev/null +++ b/src/comet_llm/logging_messages.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# ******************************************************* +# ____ _ _ +# / ___|___ _ __ ___ ___| |_ _ __ ___ | | +# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | +# | |__| (_) | | | | | | __/ |_ _| | | | | | | +# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| +# +# Sign up for free at https://www.comet.com +# Copyright (C) 2015-2023 Comet ML INC +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this package. +# ******************************************************* + + +UNABLE_TO_LOG_TO_NON_LLM_PROJECT = "Failed to send prompt to the specified project as it is not an LLM project, please specify a different project name." diff --git a/tests/unit/experiment_api/test_request_exception_wrapper.py b/tests/unit/experiment_api/test_request_exception_wrapper.py index ac28495942..28abe04fe9 100644 --- a/tests/unit/experiment_api/test_request_exception_wrapper.py +++ b/tests/unit/experiment_api/test_request_exception_wrapper.py @@ -1,3 +1,5 @@ +import json + import pytest import requests from testix import * @@ -9,6 +11,7 @@ @pytest.fixture(autouse=True) def mock_imports(patch_module): patch_module(request_exception_wrapper, "config") + patch_module(request_exception_wrapper, "failed_response") def test_wrap_no_exceptions(): @@ -49,4 +52,23 @@ def f(): with Scenario() as s: s.config.comet_url() >> COMET_CLOUD_URL with pytest.raises(exceptions.CometLLMException): - f() \ No newline at end of file + f() + + +def test_wrap__request_exception_non_llm_project_sdk_code__log_specifc_message_in_exception(): + @request_exception_wrapper.wrap() + def f(): + exception = requests.RequestException() + response = requests.Response + response.text = json.dumps({"sdk_error_code": 34323}) + exception.response = response + raise exception + + expected_log_message = "Failed to send prompt to the specified project as it is not an LLM project, please specify a different project name." + + with Scenario() as s: + s.failed_response.handle(requests.Response) >> expected_log_message + with pytest.raises(exceptions.CometLLMException) as excinfo: + f() + + assert excinfo.value.args == (expected_log_message, ) \ No newline at end of file