diff --git a/pinecone/config/openapi_configuration.py b/pinecone/config/openapi_configuration.py index fce6defc1..c3ce79a40 100644 --- a/pinecone/config/openapi_configuration.py +++ b/pinecone/config/openapi_configuration.py @@ -2,7 +2,6 @@ import logging import multiprocessing -from http import client as http_client from pinecone.exceptions import PineconeApiValueError from typing import TypedDict @@ -154,6 +153,7 @@ def __init__( self.logger_file = None """Debug file location """ + # Initialize debug directly without using the property setter self.debug = False """Debug switch """ @@ -288,7 +288,7 @@ def debug(self): :param value: The debug status, True or False. :type: bool """ - return self.__debug + return self._debug @debug.setter def debug(self, value): @@ -297,20 +297,37 @@ def debug(self, value): :param value: The debug status, True or False. :type: bool """ - self.__debug = value - if self.__debug: - # if debug status is True, turn on debug logging + if hasattr(self, "_debug"): + previous_debug = self._debug + else: + previous_debug = None + self._debug = value + + def enable_http_logging(): + from http import client as http_client + + http_client.HTTPConnection.debuglevel = 1 + + def disable_http_logging(): + from http import client as http_client + + http_client.HTTPConnection.debuglevel = 0 + + def set_default_log_level(c): + for _, logger in c.logger.items(): + logger.setLevel(logging.WARNING) + + if self._debug: for _, logger in self.logger.items(): logger.setLevel(logging.DEBUG) - # turn on http_client debug - http_client.HTTPConnection.debuglevel = 1 + enable_http_logging() + elif previous_debug is True and self._debug is False: + set_default_log_level(self) + disable_http_logging() else: - # if debug status is False, turn off debug logging, - # setting log level to default `logging.WARNING` - for _, logger in self.logger.items(): - logger.setLevel(logging.WARNING) - # turn off http_client debug - http_client.HTTPConnection.debuglevel = 0 + # On the initial call, we don't need to do anything to http + # logging, since it's not enabled by default. + set_default_log_level(self) @property def logger_format(self): diff --git a/pinecone/utils/error_handling.py b/pinecone/utils/error_handling.py index 5cdaaaf46..c18090eb2 100644 --- a/pinecone/utils/error_handling.py +++ b/pinecone/utils/error_handling.py @@ -1,7 +1,11 @@ import inspect from functools import wraps -from urllib3.exceptions import MaxRetryError, ProtocolError + +class ProtocolError(Exception): + """Raised when there is a protocol error in the connection.""" + + pass def validate_and_convert_errors(func): @@ -9,15 +13,21 @@ def validate_and_convert_errors(func): def inner_func(*args, **kwargs): try: return func(*args, **kwargs) - except MaxRetryError as e: - if isinstance(e.reason, ProtocolError): + except Exception as e: + # Lazy import of urllib3 exceptions + from urllib3.exceptions import MaxRetryError, ProtocolError as Urllib3ProtocolError + + if isinstance(e, MaxRetryError): + if isinstance(e.reason, Urllib3ProtocolError): + raise ProtocolError(f"Failed to connect to {e.url}") from e + else: + raise e from e + elif isinstance(e, Urllib3ProtocolError): raise ProtocolError( - f"Failed to connect to {e.url}; did you specify the correct index name?" + "Connection failed. Please verify that the index host is correct and accessible." ) from e else: - raise - except ProtocolError as e: - raise ProtocolError("Failed to connect; did you specify the correct index name?") from e + raise e from e # Override signature sig = inspect.signature(func)