o
    iI%                     @   s   d Z ddlmZmZmZmZmZ ddlZddlZddl	Z	ddl
mZ er4ddlmZ ddlZddlm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ej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ej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ejf ddfddZeded	edeede	jej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.coder	   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__ K/home/livre-enfant/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__D   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%   K   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   S      zAPIError._get_statusc                 C   r,   )Nr   r-   r.   )r   r   r   r    r    r!   r   ^   r1   zAPIError._get_messagec                 C   s   | d| di  dd S )Nr   r-   )r/   )r   r   r    r    r!   r   i   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_recordn   s   zAPIError._to_replay_recordc                 C   s   |j dkrdS t|tjr,z
|  | }W n< tjjy+   |j}||j	d}Y n)w t|t
jrKz| }W n t
jjyJ   |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   httpxr
   readjsondecoderJSONDecodeErrortextreason_phraserequests
exceptionsreasonbody_segmentsr/   raise_error)clsr   r   r   r    r    r!   raise_for_response|   s,   


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                    sx  t |tjr4|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rRt
|drR|jdkrEdS |jd di }|j}n^zMddl}|}t
|dra|j}t ||jr|jdkroW 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   r
   r7   areadr:   r;   r<   r=   r>   hasattrrB   r/   aiohttprK   ClientResponser   client_exceptionsContentTypeErrorrA   
ValueErrortypeImportErrorraise_error_async)rD   r   r   r   r7   rN   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|||| |||rF   rG   rJ   r    r    r!   rU      s   zAPIError.raise_error_async)N)"__name__
__module____qualname____doc__int__annotations__r   r?   r
   r8   r   r   strr   r   r   tupler   dictr'   staticmethodr%   r   r   r   r   
StringDictr3   classmethodrE   rC   rW   rU   __classcell__r    r    r   r!   r      s   
 
  :r   c                   @      e Zd ZdZdS )rH   z%Client error raised by the GenAI API.NrX   rY   rZ   r[   r    r    r    r!   rH         rH   c                   @   re   )rI   z%Server error raised by the GenAI API.Nrf   r    r    r    r!   rI     rg   rI   c                   @   re   ) UnknownFunctionCallArgumentErrorzWRaised when the function call argument cannot be converted to the parameter annotation.Nrf   r    r    r    r!   rh     rg   rh   c                   @   re   )UnsupportedFunctionErrorz*Raised when the function is not supported.Nrf   r    r    r    r!   ri   #  rg   ri   c                   @   re   )FunctionInvocationErrorzDRaised when the function cannot be invoked with the given arguments.Nrf   r    r    r    r!   rj   (  rg   rj   c                   @   re   )UnknownApiResponseErrorz?Raised when the response from the API cannot be parsed as JSON.Nrf   r    r    r    r!   rk   -  rg   rk   )r[   typingr   r   r   r   r   r8   r:   r?    r   replay_api_clientr	   rN   !google.auth.aio.transport.aiohttpr
   r   r*   r   rH   rI   rR   rh   ri   rj   rk   ExperimentalWarningr    r    r    r!   <module>   s&    v
