Skip to content

Commit

Permalink
Reorganize request exception handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkuzmik committed Nov 29, 2023
2 parents 2438f54 + 6af8e11 commit c4f850d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/comet_llm/experiment_api/failed_response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,25 @@
# LICENSE file in the root directory of this package.
# *******************************************************

import collections
import json
from typing import NoReturn

import collections
import requests # type: ignore

from .. import backend_error_codes, logging_messages, exceptions
from .. import backend_error_codes, exceptions, logging_messages

_SDK_ERROR_CODES_LOGGING_MESSAGE = collections.defaultdict(
lambda: logging_messages.FAILED_TO_SEND_DATA_TO_SERVER,
{
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)

def handle(exception: requests.RequestException) -> NoReturn:
response = exception.response
sdk_error_code = json.loads(response.text)["sdk_error_code"]
error_message = _SDK_ERROR_CODES_LOGGING_MESSAGE[sdk_error_code]

raise exceptions.CometLLMException(error_message) from exception

10 changes: 6 additions & 4 deletions src/comet_llm/experiment_api/request_exception_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ 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."
) from exception

if exception.response is None:
raise exceptions.CometLLMException(logging_messages.FAILED_TO_SEND_DATA_TO_SERVER) from exception

raise exceptions.CometLLMException(
logging_messages.FAILED_TO_SEND_DATA_TO_SERVER
) from exception

failed_response_handler.handle(exception)

return wrapper
Expand All @@ -67,7 +69,7 @@ def _debug_log(exception: requests.RequestException) -> None:

if exception.response is not None:
LOGGER.debug(f"Response:\n{pformat(vars(exception.response))}")
except:
except Exception:
# Make sure we won't fail on attempt to debug.
# It's mainly for tests when response object can be mocked
pass
2 changes: 1 addition & 1 deletion tests/unit/experiment_api/test_failed_response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import box
import pytest

from testix import *

from comet_llm.exceptions import exceptions
from comet_llm.experiment_api import failed_response_handler


def test_wrap__request_exception_non_llm_project_sdk_code__log_specifc_message_in_exception():
exception = Exception()
exception.response = box.Box(text=json.dumps({"sdk_error_code": 34323}))
Expand Down

0 comments on commit c4f850d

Please sign in to comment.