o
    kDi                     @   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	m
Z
 er*ddlmZ ddl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)ReplayResponsec                       s  e Zd ZU dZeed< e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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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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	   zaiohttp.ClientResponse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__ S/home/air/goalskill_t/back/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__8   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#   ?   s   
"zAPIError._rebuildc                 C      | d| di  dd S )Nr   errorgetr   r   r   r   r   r   G      zAPIError._get_statusc                 C   r*   )Nr   r+   r,   r.   r   r   r   r   L   r/   zAPIError._get_messagec                 C   r*   )Nr   r+   r,   r.   r   r   r   r   Q   r/   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   r0   )r   r   r   r   _to_replay_recordV   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 |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readjsondecoderJSONDecodeErrortextreason_phrasebody_segmentsr-   raise_error)clsr   r   r   r   r   r   raise_for_responsed   s   

zAPIError.raise_for_responser5   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r@   r5   r   r   r   r   r   r?   {   s
   zAPIError.raise_errorc                    sl  d}d}t |tjr8|jdkrdS z| I dH  | }W n tjjy3   |j}||j	d}Y nw |j}nrt
|drVt
|drV|jdkrIdS |jd di }|j}nTzCddl}t ||jr|jdkriW 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 )	r2   r   Nr3   r4   r>   r5   r+   zUnsupported response type: )r   r6   r7   r5   areadr9   r:   r;   r<   r=   hasattrr>   r-   aiohttpClientResponser   client_exceptionsContentTypeErrorreason
ValueErrortypeImportErrorraise_error_async)r@   r   r5   r   r   rI   r   r   r   raise_for_async_response   sV   




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|||| |||rB   rC   rF   r   r   r   rQ      s   zAPIError.raise_error_async)N)!__name__
__module____qualname____doc__int__annotations__r   r6   r7   r   r   strr   r   r   tupler   dictr%   staticmethodr#   r   r   r   r   
StringDictr1   classmethodrA   r?   rR   rQ   __classcell__r   r   r   r   r
      s~   
 

4r
   c                   @      e Zd ZdZdS )rD   z%Client error raised by the GenAI API.NrS   rT   rU   rV   r   r   r   r   rD          rD   c                   @   r`   )rE   z%Server error raised by the GenAI API.Nra   r   r   r   r   rE      rb   rE   c                   @   r`   ) UnknownFunctionCallArgumentErrorzWRaised when the function call argument cannot be converted to the parameter annotation.Nra   r   r   r   r   rc      rb   rc   c                   @   r`   )UnsupportedFunctionErrorz*Raised when the function is not supported.Nra   r   r   r   r   rd      rb   rd   c                   @   r`   )FunctionInvocationErrorzDRaised when the function cannot be invoked with the given arguments.Nra   r   r   r   r   re      rb   re   c                   @   r`   )UnknownApiResponseErrorz?Raised when the response from the API cannot be parsed as JSON.Nra   r   r   r   r   rf     rb   rf   )rV   typingr   r   r   r   r   r6   r9    r   replay_api_clientr	   rI   r(   r
   rD   rE   rN   rc   rd   re   rf   ExperimentalWarningr   r   r   r   <module>   s"    L
