Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions pinecone/config/openapi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import multiprocessing

from http import client as http_client
from pinecone.exceptions import PineconeApiValueError
from typing import TypedDict

Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
24 changes: 17 additions & 7 deletions pinecone/utils/error_handling.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
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):
@wraps(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)
Expand Down
Loading