o
    RDiR                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 ddl
mZ ddlmZ ddlmZmZ dd	lmZ erLdd
lmZ ddlmZ eeZejddZedZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd de#Z$G dd de#Z%G dd de#Z&G dd  d e#Z'G d!d" d"e#Z(G d#d$ d$e#Z)G d%d& d&e%Z*G d'd( d(eZ+G d)d* d*eZ,G d+d, d,eZ-G d-d. d.eZ.G d/d0 d0eZ/G d1d2 d2eZ0G d3d4 d4eZ1G d5d6 d6eZ2G d7d8 d8eZ3G d9d: d:eZ4G d;d< d<eZ5G d=d> d>eZ6G d?d@ d@eZ7G dAdB dBeZ8G dCdD dDeZ9G dEdF dFeZ:G dGdH dHeZ;G dIdJ dJe;Z<G dKdL dLeZ=dS )M    )annotationsN)	getLogger)TYPE_CHECKINGAny   )ER_HTTP_GENERAL_ERROR)SecretDetector)TelemetryDataTelemetryField)get_time_millis)SnowflakeConnection)SnowflakeCursor	snowflake	connectorz^(\d{6,})(?: \((\S+)\))?:c                
      s   e Zd ZdZddddddddejdf
dB fddZdCddZdCddZe	dCddZ
dDd d!ZdEd#d$ZdFd'd(ZdGd)d*Ze	dHd1d2Ze		dIdJd5d6Ze	dKd8d9Ze	dLd;d<Ze	dMd>d?Ze	dNd@dAZ  ZS )OErrorzBase Snowflake exception class.NTmsg
str | Noneerrno
int | Nonesqlstatesfqidquerydone_format_msgbool | None
connectionSnowflakeConnection | NonecursorSnowflakeCursor | Noneerrtyper
   send_telemetryboolreturnNonec                   sL  t  | || _|| _|pd| _|pd| _|| _|| _|	| _|
| _	| jr+t
|}nd| _d }| jdkr|s| jdkrl|skt tjtjfv r\| jdd| j d| j d| j | _n7| jdd| j d| j | _n'|st tjtjfv r| jdd| j d| j | _n| jdd| j | _|  | _| j	r| ||| d S d S )Nn/azUnknown error06d (z): z: )super__init__r   raw_msgr   r   r   r   r   r   RE_FORMATTED_ERRORmatchloggergetEffectiveLevelloggingINFODEBUGgenerate_telemetry_stacktracetelemetry_tracebackexception_telemetry)selfr   r   r   r   r   r   r   r   r   r   already_formatted_msg	__class__ S/var/www/Datamplify/venv/lib/python3.10/site-packages/snowflake/connector/errors.pyr(      s8   


( 
zError.__init__strc                 C  s   |   S N)__str__r4   r8   r8   r9   __repr__V   s   zError.__repr__c                 C     | j S r;   r   r=   r8   r8   r9   r<   Y      zError.__str__c               	   C  sl   t  d d } t }| D ] }t|jv r-|jt}|t j|j|d  |j|j	dd qd
t |S )N )line)	tracebackextract_stacklistconnector_base_pathfilenamefindappendFrameSummarylinenonamejoinformat_list)stack_framesfiltered_framesframesafe_path_indexr8   r8   r9   r1   \   s   
	z#Error.generate_telemetry_stacktracec                 C  s8   | j dkr| jdd| j  dS | jdkr| jdS d S )Nr$   r%   r&   )r#   )r   r   r=   r8   r8   r9   telemetry_msgs   s
   


zError.telemetry_msg&dict[str, tuple[bool, str, str] | str]c                 C  s   t jjt| ji}|  }| jr| j|t jj< | j	r"| j	|t j
j< |r*||t jj< | jr6t| j|t jj< | jr@| j|t jj< |S )z,Generate the data to send through telemetry.)r
   KEY_STACKTRACEvaluer   mask_secretsr2   rV   r   	KEY_SFQIDr   KEY_SQLSTATE
KEY_REASONr   r:   KEY_ERROR_NUMBERr   KEY_ERROR_MESSAGE)r4   telemetry_data_dictrV   r8   r8   r9   !generate_telemetry_exception_data{   s    
z'Error.generate_telemetry_exception_datatelemetry_datadict[str, Any]c                 C  s   |durE|j rG|jjsI| jj|tjj< |j|tjj< | j	j
|tjj< t }z|tj|||d W dS  tyD   tjddd Y dS w dS dS dS )zhSend telemetry data by in-band telemetry if it is enabled, otherwise send through out-of-band telemetry.N)	from_dict	timestampr   z"Cursor failed to log to telemetry.T)exc_info)telemetry_enabled
_telemetry	is_closedr   rY   r
   KEY_TYPEapplication
KEY_SOURCEr7   __name__KEY_EXCEPTIONr   _log_telemetryr	   from_telemetry_data_dictAttributeErrorr,   debug)r4   r   rb   tsr8   r8   r9   send_exception_telemetry   s&   
zError.send_exception_telemetryc                 C  sp   z(|   }|dur| |j| W dS |dur | || W dS | d| W dS  ty7   td Y dS w )z?Main method to generate and send telemetry data for exceptions.Nz"Sending exception telemetry failed)ra   rt   r   	Exceptionr,   rr   )r4   r   r   r   r`   r8   r8   r9   r3      s    

zError.exception_telemetryr   r   error_classtype[Error]error_valuedict[str, str]c              
   C  sd   | d}| d}|| d|du rdnt|| d| d| d|du r*dnt|| |d)	ae  Default error handler that raises an error.

        Args:
            connection: Connections in which the error happened.
            cursor: Cursor in which the error happened.
            error_class: Class of error that needs handling.
            error_value: A dictionary of the error details.

        Raises:
            A Snowflake error.
        r   r   r   Nr   r   r   )r   r   r   r   r   r   r   r   )getintr    )r   r   rv   rx   r   r   r8   r8   r9   default_errorhandler   s   

zError.default_errorhandlercauseError | Exceptionc              
   C  s$   t | |t||j|j|jddS )aG  Wrapper for errorhandler_wrapper, it is called with a cause instead of a dictionary.

        The dictionary is first extracted from the cause and then it's given to errorhandler_wrapper

        Args:
            connection: Connections in which the error happened.
            cursor: Cursor in which the error happened.
            cause: Error instance that we want to handle.

        Returns:
            None if no exceptions are raised by the connection's and cursor's error handlers.

        Raises:
            A Snowflake error if connection and cursor are None.
        T)r   r   r   r   )r   errorhandler_wrappertyper   r   r   )r   r}   r   r8   r8   r9   errorhandler_wrapper_from_cause   s   z%Error.errorhandler_wrapper_from_causetype[Error] | type[Exception]c                 C  s$   t | |||}|st ||dS )a`  Error handler wrapper that calls the errorhandler method.

        Args:
            connection: Connections in which the error happened.
            cursor: Cursor in which the error happened.
            error_class: Class of error that needs handling.
            error_value: An optional dictionary of the error details.

        Returns:
            None if no exceptions are raised by the connection's and cursor's error handlers.

        Raises:
            A Snowflake error if connection, or cursor are None. Otherwise it gives the
            exception to the first handler in that order.
        N)r   hand_to_other_handlererrorhandler_make_exception)r   r   rv   rx   handed_overr8   r8   r9   r     s   zError.errorhandler_wrapper	error_excc                 C  sH   t |tr|j|j|j|jd}n|j}t| |t||}|s"|dS )z@Like errorhandler_wrapper, but it takes a ready to go Exception.r   r   r   r   N)	
isinstancer   r   r   r   r   argsr   r   )r   r   r   rx   r   r8   r8   r9   )errorhandler_wrapper_from_ready_exception*  s    
z/Error.errorhandler_wrapper_from_ready_exceptiondict[str, str | bool]c                 C  sp   | dd | dur| j||f |dur(|j||f || ||| dS | dur6| | ||| dS dS )zIf possible give error to a higher error handler in connection, or cursor.

        Returns:
            Whether it error was successfully given to a handler.
        r   FNT)
setdefaultmessagesrK   errorhandler)r   r   rv   rx   r8   r8   r9   r   D  s   zError.hand_to_other_handlerc                 C  sD   | dd t| tr| |d |d|d|ddS | |S )zCHelper function to errorhandler_wrapper that creates the exception.r   Fr   r   r   r   r   )r   
issubclassr   rz   )rv   rx   r8   r8   r9   r   \  s   
z!Error.errorhandler_make_exception)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r    r!   r"   r!   r:   )r!   r   )r!   rW   )r   r   rb   rc   r!   r"   )r   r:   r   r   r   r   r!   r"   )
r   r   r   r   rv   rw   rx   ry   r!   r"   r;   )r   r   r}   r~   r   r   r!   r"   )
r   r   r   r   rv   r   rx   rc   r!   r"   )r   r   r   r   r   r~   r!   r"   )
r   r   r   r   rv   r   rx   r   r!   r    )rv   r   rx   r   r!   r~   )rm   
__module____qualname____doc__r
   SQL_EXCEPTIONr(   r>   r<   staticmethodr1   rV   ra   rt   r3   r|   r   r   r   r   r   __classcell__r8   r8   r6   r9   r      sD    
8




  "r   c                   @     e Zd ZdZdS )_Warningz!Exception for important warnings.Nrm   r   r   r   r8   r8   r8   r9   r   n      r   c                   @  r   )InterfaceErrorz.Exception for errors related to the interface.Nr   r8   r8   r8   r9   r   t  r   r   c                   @     e Zd ZdddZdS )	HttpErrorr!   r"   c                 K  s   t j| fdtji| d S )Nr   )r   r(   r
   HTTP_EXCEPTIONr4   kwargsr8   r8   r9   r(   {  s   
zHttpError.__init__Nr!   r"   rm   r   r   r(   r8   r8   r8   r9   r   z      r   c                   @  r   )DatabaseErrorz-Exception for errors related to the database.Nr   r8   r8   r8   r9   r     r   r   c                   @  r   )InternalErrorz.Exception for errors internal database errors.Nr   r8   r8   r8   r9   r     r   r   c                   @  r   )OperationalErrorz9Exception for errors related to the database's operation.Nr   r8   r8   r8   r9   r     r   r   c                   @  r   )ProgrammingErrorz(Exception for errors programming errors.Nr   r8   r8   r8   r9   r     r   r   c                   @  r   )IntegrityErrorz4Exception for errors regarding relational integrity.Nr   r8   r8   r8   r9   r     r   r   c                   @  r   )	DataErrorz<Exception for errors reporting problems with processed data.Nr   r8   r8   r8   r9   r     r   r   c                   @     e Zd ZdZdddZdS )NotSupportedErrorzCException for errors when an unsupported database feature was used.r!   r:   c                 C  r?   r;   r@   r=   r8   r8   r9   rV     rA   zNotSupportedError.telemetry_msgNr   )rm   r   r   r   rV   r8   r8   r8   r9   r     s    r   c                   @  r   )RevocationCheckErrorz9Exception for errors during certificate revocation check.r!   r"   c                 K  s*   | dd}tj| ftj|d| d S )Nr   F)r   r   )popr   r(   r
   OCSP_EXCEPTION)r4   r   r   r8   r8   r9   r(     s   
zRevocationCheckError.__init__Nr   rm   r   r   r   r(   r8   r8   r8   r9   r         r   c                   @  r   )InternalServerErrorz&Exception for 500 HTTP code for retry.r!   r"   c              	   K  >   t j| |dp	dt|dd tj|d|dd d S )Nr   zHTTP 500: Internal Server Errorr   r   r   r   r   r   r   r   r   r   r(   rz   r   r
   r   r   r8   r8   r9   r(        
zInternalServerError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )ServiceUnavailableErrorz&Exception for 503 HTTP code for retry.r!   r"   c              	   K  r   )Nr   zHTTP 503: Service Unavailabler   r   r   r   r   r   r   r8   r8   r9   r(     r   z ServiceUnavailableError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )GatewayTimeoutErrorz'Exception for 504 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 504: Gateway Timeoutr   r   r   r   r   r   r   r8   r8   r9   r(     r   zGatewayTimeoutError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )ForbiddenErrorz'Exception for 403 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 403: Forbiddenr   r   r   r   r   r   r   r8   r8   r9   r(     r   zForbiddenError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )RequestTimeoutErrorz'Exception for 408 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 408: Request Timeoutr   r   r   r   r   r   r   r8   r8   r9   r(     r   zRequestTimeoutError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )
BadRequestz'Exception for 400 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 400: Bad Requestr   r   r   r   r   r   r   r8   r8   r9   r(     r   zBadRequest.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )BadGatewayErrorz'Exception for 502 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 502: Bad Gatewayr   r   r   r   r   r   r   r8   r8   r9   r(     r   zBadGatewayError.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )MethodNotAllowedz'Exception for 405 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 405: Method not allowedr   r   r   r   r   r   r   r8   r8   r9   r(   "  r   zMethodNotAllowed.__init__Nr   r   r8   r8   r8   r9   r     r   r   c                   @  r   )TooManyRequestsz'Exception for 429 HTTP error for retry.r!   r"   c              	   K  r   )Nr   zHTTP 429: Too Many Requestsr   r   r   r   r   r   r   r8   r8   r9   r(   0  r   zTooManyRequests.__init__Nr   r   r8   r8   r8   r9   r   -  r   r   c                   @  r   )RefreshTokenErrorr!   r"   c                 K  s4   t j| |dp	d|d|d|dd d S )Nr   zToken Refresh Requiredr   r   r   r   )r   r(   rz   r   r8   r8   r9   r(   <  s   
zRefreshTokenError.__init__Nr   r   r8   r8   r8   r9   r   ;  r   r   c                   @  r   )OtherHTTPRetryableErrorz)Exception for other HTTP error for retry.r!   r"   c              	   K  sP   | dd}tj| | dpd| t| dd tj| d| dd	 d S )
Ncoder$   r   zHTTP r   r   r   r   r   )rz   r   r(   r   r
   r   )r4   r   r   r8   r8   r9   r(   I  s   
z OtherHTTPRetryableError.__init__Nr   r   r8   r8   r8   r9   r   F  r   r   c                      s"   e Zd ZdZd fddZ  ZS )	MissingDependencyErrorz*Exception for missing extras dependencies.
dependencyr:   r!   r"   c                   s   t  jd| d d S )NzMissing optional dependency: r@   )r'   r(   )r4   r   r6   r8   r9   r(   X  s   zMissingDependencyError.__init__)r   r:   r!   r"   )rm   r   r   r   r(   r   r8   r8   r6   r9   r   U  s    r   c                   @  r   )BindUploadErrorz:Exception for bulk array binding stage optimization fails.Nr   r8   r8   r8   r9   r   \  r   r   c                   @  r   )RequestExceedMaxRetryErrorz^Exception for REST call to remote storage API exceeding maximum retries with transient errors.Nr   r8   r8   r8   r9   r   b  r   r   c                   @  r   )TokenExpiredErrorz]Exception for REST call to remote storage API failed because of expired authentication token.Nr   r8   r8   r8   r9   r   h  r   r   c                   @  r   )PresignedUrlExpiredErrorzVException for REST call to remote storage API failed because of expired presigned URL.Nr   r8   r8   r8   r9   r   n  r   r   c                   @  r   )ConfigSourceErrorzjConfiguration source related errors.

    Examples are environmental variable and configuration file.
    Nr   r8   r8   r8   r9   r   t      r   c                   @  r   )MissingConfigOptionErrorzWhen a configuration option is missing from the final, resolved configurations.

    This is a special-case of ConfigSourceError.
    Nr   r8   r8   r8   r9   r   {  r   r   c                   @  r   )ConfigManagerErrorzhConfiguration manager related errors.

    This means that ConfigManager is misused by a developer.
    Nr   r8   r8   r8   r9   r     r   r   )>
__future__r   r.   osrerE   r   typingr   r   	errorcoder   secret_detectorr   	telemetryr	   r
   	time_utilr   r   r   r   r   rm   r,   pathrO   rH   compiler*   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   <module>   sd   
  U	