"""Exception classes for es_clientThis module defines custom exceptions for es_client, used in:class:`~es_client.builder.Builder`, :mod:`~es_client.logging`,:mod:`~es_client.config`, :mod:`~es_client.schemacheck`, and:mod:`~es_client.utils` to handle specific error conditions.Classes: ESClientException: Base class for non-Elasticsearch exceptions. ConfigurationError: Exception for misconfiguration issues. MissingArgument: Exception for missing required arguments. NotMaster: Exception for non-master node connections. LoggingException: Exception for logging configuration failures. SchemaException: Base class for schema-related exceptions. FailedValidation: Exception for SchemaCheck validation failures."""importtypingastfromcopyimportdeepcopyfrom.defaultsimportKEYS_TO_REDACT
[docs]defpassword_filter(data:t.Dict)->t.Dict:""" Redact sensitive values from a configuration dictionary. Args: data (dict): Configuration dictionary to process. Returns: dict: A deep copy of `data` with sensitive values (keys in :data:`~es_client.defaults.KEYS_TO_REDACT`) replaced with 'REDACTED'. Recursively traverses `data`, replacing values of keys listed in :data:`~es_client.defaults.KEYS_TO_REDACT` (e.g., 'password', 'api_key') with 'REDACTED' for secure logging. Example: >>> data = {'user': 'test', 'password': 'secret', 'nested': {'api_key': 'key'}} >>> filtered = password_filter(data) >>> filtered {'user': 'test', 'password': 'REDACTED', 'nested': {'api_key': 'REDACTED'}} >>> data['password'] # Original unchanged 'secret' """defiterdict(mydict):forkey,valueinmydict.items():ifisinstance(value,dict):iterdict(value)elifkeyinKEYS_TO_REDACT:mydict.update({key:"REDACTED"})returnmydictreturniterdict(deepcopy(data))
[docs]classESClientException(Exception):""" Base class for non-Elasticsearch exceptions in es_client. Example: >>> raise ESClientException('Generic error') Traceback (most recent call last): ... es_client.exceptions.ESClientException: Generic error """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classConfigurationError(ESClientException):""" Exception for configuration errors in es_client. Raised when invalid settings are detected, such as malformed hosts or YAML files. Example: >>> raise ConfigurationError('Invalid host schema') Traceback (most recent call last): ... es_client.exceptions.ConfigurationError: Invalid host schema """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classMissingArgument(ESClientException):""" Exception for missing required arguments in es_client. Raised when a necessary parameter is not provided. Example: >>> raise MissingArgument('Missing username') Traceback (most recent call last): ... es_client.exceptions.MissingArgument: Missing username """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classNotMaster(ESClientException):""" Exception for non-master node connections in es_client. Raised when a node is not the elected master and master_only is True. Example: >>> raise NotMaster('Not the master node') Traceback (most recent call last): ... es_client.exceptions.NotMaster: Not the master node """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classLoggingException(ESClientException):""" Exception for logging configuration failures in es_client. Raised when logging cannot be configured properly. Example: >>> raise LoggingException('Logging setup failed') Traceback (most recent call last): ... es_client.exceptions.LoggingException: Logging setup failed """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classSchemaException(ESClientException):""" Base class for schema-related exceptions in es_client. Example: >>> raise SchemaException('Schema error') Traceback (most recent call last): ... es_client.exceptions.SchemaException: Schema error """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"
[docs]classFailedValidation(SchemaException):""" Exception for SchemaCheck validation failures in es_client. Raised when :class:`~es_client.schemacheck.SchemaCheck` validation fails. Example: >>> raise FailedValidation('Invalid value detected') Traceback (most recent call last): ... es_client.exceptions.FailedValidation: Invalid value detected """def__repr__(self)->str:""" Return a string representation of the exception. Returns: str: Formatted string with class name and redacted message. """message=(password_filter(self.args[0])ifself.argsandisinstance(self.args[0],dict)elseself.args[0]ifself.argselse'')returnf"<{self.__class__.__name__} message='{message}'>"