o
    pkj'                     @   s"  d Z ddlmZmZmZmZmZ ddlZddlZzddl	Z	W n e
y)   dZ	Y nw ddlmZ erFddlmZ ddlZddlmZ ddlZe	du rNejfneje	jf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ejZdS )z Error classes for the GenAI SDK.    )AnyCallableOptionalTYPE_CHECKINGUnionN   )_common)ReplayResponse)Responsec                       s  e Zd ZU dZeed< eddejdf ed< dZ	e
e ed< dZe
e ed	< 	d&ded
ede
eddejdf  f fddZdeed eeeef  f fddZedeeef dd fddZd
edefddZd
edefddZd
edefddZdejfddZededejdf ddfddZeded
ede
edejdf  ddfdd Zededejd!df ddfd"d#Zeded
ede
edejd!f  ddfd$d%Z  Z S )'APIErrorz'General errors raised by the GenAI API.codezrequests.Responser	   AsyncAuthorizedSessionResponseresponseNstatusmessageresponse_jsonc                    s|   t |trt|dkr|d }|| _|| _| || _| || _|r%|n| 	|| _
t | j
 d| j d| j  d S )Nr   r    . )
isinstancelistlenr   details_get_messager   _get_statusr   	_get_coder   super__init__)selfr   r   r   	__class__ Q/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/google/genai/errors.pyr   ;   s   &zAPIError.__init__return).r   c                 C   s   | j  }| jj|ffS )zGReturns a tuple that can be used to reconstruct the error for pickling.)__dict__copyr   _rebuild)r   stater    r    r!   
__reduce__S   s   
zAPIError.__reduce__r&   c                 C   s<   t t }|j|  t||j d|j d|j  |S )z"Rebuilds the error from the state.r   r   )	r   __new__r#   update	Exceptionr   r   r   r   )r&   objr    r    r!   r%   Z   s   
"zAPIError._rebuildc                 C   8   z| d| di  dd }|W S  ty   Y d S w )Nr   errorgetAttributeError)r   r   r   r    r    r!   r   b      zAPIError._get_statusc                 C   r,   )Nr   r-   r.   )r   r   r   r    r    r!   r   m   r1   zAPIError._get_messagec                 C   s   | d| di  dd S )Nr   r-   )r/   )r   r   r    r    r!   r   x   s   zAPIError._get_codec                 C   s   d| j | j| jdiS )zReturns a dictionary representation of the error for replay recording.

    details is not included since it may expose internal information in the
    replay file.
    r-   r   r   r   r2   )r   r    r    r!   _to_replay_record}   s   zAPIError._to_replay_recordc                 C   s   |j dkrdS t|tr+z
|  | }W nD tjjy*   |j}||jd}Y n1w t	
  }durRt||jrRz| }W n |jjyQ   |j|jd}Y n
w |jd di }| |j || dS )PRaises an error with detailed error message if the response has an error status.   Nr   r   r   r-   )status_coder   _HTTPX_RESPONSE_TYPESreadjsondecoderJSONDecodeErrortextreason_phraser   loaded_requestsr
   
exceptionsreasonbody_segmentsr/   raise_error)clsr   r   r   requestsr    r    r!   raise_for_response   s0   



zAPIError.raise_for_responser7   c                 C   sT   d|  kr
dk rn nt |||d|  krdk r$n nt|||| |||a  Raises an appropriate APIError subclass based on the status code.

    Args:
      status_code: The HTTP status code of the response.
      response_json: The JSON body of the response, or a dict containing error
        details.
      response: The original response object.

    Raises:
      ClientError: If the status code is in the 4xx range.
      ServerError: If the status code is in the 5xx range.
      APIError: For other error status codes.
    i  i  iX  ClientErrorServerErrorrD   r7   r   r   r    r    r!   rC      s
   zAPIError.raise_errorzaiohttp.ClientResponsec                    sv  t |tr3|jdkrdS z| I dH  | }W n tjjy.   |j}||jd}Y nw |j}n|t	|drQt	|drQ|jdkrDdS |j
d di }|j}n^zMddl}|}t	|dr`|j}t ||jr|jdkrnW dS z	| I dH }W n |jjy   | I dH }||jd}Y nw |j}n	td	t| W n ty   td	t| w | |||I dH  dS )
r4   r5   Nr6   rB   r7   r   r-   	_responsezUnsupported response type: )r   r8   r7   areadr:   r;   r<   r=   r>   hasattrrB   r/   aiohttprL   ClientResponser   client_exceptionsContentTypeErrorrA   
ValueErrortypeImportErrorraise_error_async)rD   r   r   r   r7   rO   unwrapped_responser    r    r!   raise_for_async_response   sX   






z!APIError.raise_for_async_responsec                    sV   d|  krdk rn nt |||d|  krdk r%n nt|||| |||rG   rH   rK   r    r    r!   rV     s   zAPIError.raise_error_async)N)!__name__
__module____qualname____doc__int__annotations__r   httpxr
   r   r   strr   r   r   tupler   dictr'   staticmethodr%   r   r   r   r   
StringDictr3   classmethodrF   rC   rX   rV   __classcell__r    r    r   r!   r   .   s   
 
# :r   c                   @      e Zd ZdZdS )rI   z%Client error raised by the GenAI API.NrY   rZ   r[   r\   r    r    r    r!   rI   &      rI   c                   @   rg   )rJ   z%Server error raised by the GenAI API.Nrh   r    r    r    r!   rJ   +  ri   rJ   c                   @   rg   ) UnknownFunctionCallArgumentErrorzWRaised when the function call argument cannot be converted to the parameter annotation.Nrh   r    r    r    r!   rj   0  ri   rj   c                   @   rg   )UnsupportedFunctionErrorz*Raised when the function is not supported.Nrh   r    r    r    r!   rk   5  ri   rk   c                   @   rg   )FunctionInvocationErrorzDRaised when the function cannot be invoked with the given arguments.Nrh   r    r    r    r!   rl   :  ri   rl   c                   @   rg   )UnknownApiResponseErrorz?Raised when the response from the API cannot be parsed as JSON.Nrh   r    r    r    r!   rm   ?  ri   rm   )r\   typingr   r   r   r   r   r_   r:   httpx2rU    r   replay_api_clientr	   rO   !google.auth.aio.transport.aiohttpr
   r   rE   r8   r*   r   rI   rJ   rS   rj   rk   rl   rm   ExperimentalWarningr    r    r    r!   <module>   s4    y
