
    Li                        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e      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Ze	j,                  Zy)z Error classes for the GenAI SDK.    )AnyOptionalTYPE_CHECKINGUnionN   )_common)ReplayResponsec                   
    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
Zd	edefdZd	edefdZd	edefdZdej&                  fdZededej                  f   ddfd       Zeded	ede
edej                  df      ddfd       Zededej                  df   ddfd       Zeded	ede
edej                  df      ddfd       Z xZS )APIErrorz'General errors raised by the GenAI API.coder	   zaiohttp.ClientResponseresponseNstatusmessageresponse_jsonc                 ^   t        |t              rt        |      dk(  r|d   }|| _        || _        | j                  |      | _        | j                  |      | _        |r|n| j                  |      | _
        t        | 1  | j                   d| j                   d| j                          y )Nr   r    z. )
isinstancelistlenr   details_get_messager   _get_statusr   	_get_coder   super__init__)selfr   r   r   	__class__s       Q/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/google/genai/errors.pyr   zAPIError.__init__%   s     -&3}+=+B#A&mDM DL$$]3DL""=1DK$.."?DI	G		{!DKK=4<<.AB    returnc                 f    |j                  d|j                  di       j                  dd             S )Nr   errorgetr   r   s     r   r   zAPIError._get_status8   s4    -##GR044XtD r   c                 f    |j                  d|j                  di       j                  dd             S )Nr   r"   r#   r%   s     r   r   zAPIError._get_message=   s4    =$$Wb155iF r   c                 f    |j                  d|j                  di       j                  dd             S )Nr   r"   r#   r%   s     r   r   zAPIError._get_codeB   s4    !!'2.2264@ r   c                 N    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   s    r   _to_replay_recordzAPIError._to_replay_recordG   s+     	II||kk
 r   c                    |j                   dk(  ryt        |t        j                        r"	 |j	                          |j                         }n|j                  d   j                  di       }| j                  |j                   ||       y# t
        j                  j                  $ r |j                  }||j                  d}Y Xw xY w)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   s       r   raise_for_responsezAPIError.raise_for_responseU   s    
 s"(ENN+
  ,,Q/33GR@mOOH((-B \\)) 
--,,

s    B
 
8CCr/   c                     d|cxk  rdk  rn nt        |||      d|cx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:   r/   r   r   s       r   r9   zAPIError.raise_errorl   sK    , kC]H==		!c	!]H==]H55r   c                   K   d}d}t        |t        j                        rF|j                  dk(  ry	 |j	                          d{    |j                         }|j                  }nt        |d      rHt        |d      r<|j                  dk(  ry|j                  d   j                  di       }|j                  }ni	 ddl}t        ||j                        r6|j                  dk(  ry	 |j                          d{   }|j                  }nt'        dt)        |             	 | j-                  |||       d{    y7 # t
        j                  j                  $ r |j                  }||j                  d}Y #w xY w7 # |j                   j"                  $ r+ |j                          d{  7  }||j$                  d}Y w xY w# t*        $ r t'        dt)        |             w xY w7 w)	r,   r   Nr-   r.   r8   r/   r"   zUnsupported response type: )r   r0   r1   r/   areadr3   r4   r5   r6   r7   hasattrr8   r$   aiohttpClientResponser   client_exceptionsContentTypeErrorreason
ValueErrortypeImportErrorraise_error_async)r:   r   r/   r   r   rE   s         r   raise_for_async_responsez!APIError.raise_for_async_response   s     KM(ENN+				$
nn  ((k	?	+-1 
			$,,Q/33GR@m((kIh 6 67__#"*--/1M !+8h8HIJ
J  

]H
EEEK 	\\)) 
--,,

, 2**;; $MMO++G""//M  I6tH~6FGHHI Fs   /G0E E E A!G0<)G
 %G0'F :F;F ?$G
 #G0:G.;G0 E 8E>:G0=E>>G0F ,G/F20GG
 GG
 
!G++G0c                    K   d|cxk  rdk  rn nt        |||      d|cxk  rdk  rn nt        |||       | |||      wr=   r>   rA   s       r   rM   zAPIError.raise_error_async   sO     & kC]H==		!c	!]H==]H55s   AA)N)__name__
__module____qualname____doc__int__annotations__r   r0   r1   r   r   strr   r   r   r   r   r   r   
StringDictr*   classmethodr;   r9   rN   rM   __classcell__)r   s   @r   r   r      s   /)"ENN4LLMM&(3-'8C= CC C 
 %..2JJ
K	C&s s 
  
S S 
!3!3  C+U^^;<CC C, 66 6 
 %..2JJ
K	6 6 68 2F
ENN,D
D2F
 2F 2Fh 66,/6;C
 %..2JJ
K<6 	6 6r   r   c                       e Zd ZdZy)r?   z%Client error raised by the GenAI API.NrP   rQ   rR   rS    r   r   r?   r?          -r   r?   c                       e Zd ZdZy)r@   z%Server error raised by the GenAI API.Nr[   r\   r   r   r@   r@      r]   r   r@   c                       e Zd ZdZy) UnknownFunctionCallArgumentErrorzWRaised when the function call argument cannot be converted to the parameter annotation.Nr[   r\   r   r   r`   r`      s    _r   r`   c                       e Zd ZdZy)UnsupportedFunctionErrorz*Raised when the function is not supported.Nr[   r\   r   r   rb   rb      s    2r   rb   c                       e Zd ZdZy)FunctionInvocationErrorzDRaised when the function cannot be invoked with the given arguments.Nr[   r\   r   r   rd   rd      s    Lr   rd   c                       e Zd ZdZy)UnknownApiResponseErrorz?Raised when the response from the API cannot be parsed as JSON.Nr[   r\   r   r   rf   rf      s    Gr   rf   )rS   typingr   r   r   r   r0   r3    r   replay_api_clientr	   rE   	Exceptionr   r?   r@   rJ   r`   rb   rd   rf   ExperimentalWarningr\   r   r   <module>rl      s     ' 6 6    /y6y y6x( 
( 
z 
z 
j 
j  11 r   