o
    i                     @   s   d Z ddl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OptionalTYPE_CHECKINGUnionN   )_common)ReplayResponsec                       sd  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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    z. )
isinstancelistlenr   details_get_messager   _get_statusr   	_get_coder
   super__init__)selfr
   r   r   	__class__ Y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/google/genai/errors.pyr   %   s   &zAPIError.__init__returnc                 C      | d| di  dd S )Nr   errorgetr   r   r   r   r   r   8      zAPIError._get_statusc                 C   r   )Nr   r    r!   r#   r   r   r   r   =   r$   zAPIError._get_messagec                 C   r   )Nr
   r    r!   r#   r   r   r   r   B   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   r%   )r   r   r   r   _to_replay_recordG   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_responseU   s   

zAPIError.raise_for_responser*   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r5   r*   r   r   r   r   r   r4   l   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 )	r'   r   Nr(   r)   r3   r*   r    zUnsupported response type: )r   r+   r,   r*   areadr.   r/   r0   r1   r2   hasattrr3   r"   aiohttpClientResponser   client_exceptionsContentTypeErrorreason
ValueErrortypeImportErrorraise_error_async)r5   r   r*   r   r   r>   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|||| |||r7   r8   r;   r   r   r   rF      s   zAPIError.raise_error_async)N)__name__
__module____qualname____doc__int__annotations__r   r+   r,   r   r   strr   r   r   r   r   r   r   
StringDictr&   classmethodr6   r4   rG   rF   __classcell__r   r   r   r   r	      st   
 
4r	   c                   @      e Zd ZdZdS )r9   z%Client error raised by the GenAI API.NrH   rI   rJ   rK   r   r   r   r   r9          r9   c                   @   rR   )r:   z%Server error raised by the GenAI API.NrS   r   r   r   r   r:      rT   r:   c                   @   rR   ) UnknownFunctionCallArgumentErrorzWRaised when the function call argument cannot be converted to the parameter annotation.NrS   r   r   r   r   rU      rT   rU   c                   @   rR   )UnsupportedFunctionErrorz*Raised when the function is not supported.NrS   r   r   r   r   rV      rT   rV   c                   @   rR   )FunctionInvocationErrorzDRaised when the function cannot be invoked with the given arguments.NrS   r   r   r   r   rW      rT   rW   c                   @   rR   )UnknownApiResponseErrorz?Raised when the response from the API cannot be parsed as JSON.NrS   r   r   r   r   rX      rT   rX   )rK   typingr   r   r   r   r+   r.    r   replay_api_clientr   r>   	Exceptionr	   r9   r:   rC   rU   rV   rW   rX   ExperimentalWarningr   r   r   r   <module>   s"    =
