o
    2ôjW  ã                   @   s\  d dl Z d dlmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ ee jd�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G dd„ deƒZG dd„ deƒZdejfdd„Zdd„ Zdejfdd „Zd!d"„ Z	#d.dejd$ee d%efd&d'„Zd(d$„ Z d)ejfd*d+„Z!d,d-„ Z"dS )/é    N)Ú
HTTPStatus)ÚOptional)Ú	HTTPError)ÚMODELSCOPE_REQUEST_ID)Ú
get_logger)Ú	log_levelc                   @   ó   e Zd ZdS )ÚNotSupportErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/hub/errors.pyr	      ó    r	   c                   @   r   )ÚNoValidRevisionErrorNr
   r   r   r   r   r      r   r   c                   @   r   )ÚNotExistErrorNr
   r   r   r   r   r      r   r   c                   @   r   )ÚRequestErrorNr
   r   r   r   r   r      r   r   c                   @   r   )ÚGitErrorNr
   r   r   r   r   r       r   r   c                   @   r   )ÚInvalidParameterNr
   r   r   r   r   r   $   r   r   c                   @   r   )ÚNotLoginExceptionNr
   r   r   r   r   r   (   r   r   c                   @   r   )ÚFileIntegrityErrorNr
   r   r   r   r   r   ,   r   r   c                   @   r   )ÚFileDownloadErrorNr
   r   r   r   r   r   0   r   r   Úresponsec                 C   s   t | jjv r| jjt  S dS )NÚ )r   ÚrequestÚheaders©r   r   r   r   Úget_request_id4   s   r   c                 C   s   | d t jko
| d S )zš Check the request is ok

    Args:
        rsp (Response): The request response body

    Returns:
       bool: `True` if success otherwise `False`.
    ÚCodeZSuccess)r   ÚOK©Úrspr   r   r   Úis_ok;   s   	r#   c                 C   s.   d| j  dd¡v r|  ¡ }|S | j d¡}|S )Nzapplication/jsonzcontent-typer   úutf-8)r   ÚgetÚjsonÚcontentÚdecode)r   Úmessager   r   r   Ú_decode_response_errorG   s
   ÿr*   c              
   C   sJ   z|   ¡  W d S  ty$ } zt| ƒ}td|||t| ƒf ƒ|‚d }~ww )NzHRequest %s with body: %s exception, Response details: %s, request id: %s)Úraise_for_statusr   r*   r   )r   ÚurlZrequest_bodyÚerrorr)   r   r   r   Úhandle_http_post_errorO   s   þÿý€þr.   TÚraise_on_errorÚreturnc                 C   s  d}t | jtƒr z| j d¡}W n ty   | j d¡}Y nw | j}t| ƒ}d| jkr1d| }n?d| jkrA|d u r>d}	 n2d}n/d	| j  krLd
k rYn nd| j||| jf }nd
| j  krddk rpn n
d| j||| jf }|r|r| |¡ t	|| d�‚| jS )Nr   r$   ú
iso-8859-1é”  z%The request model: %s does not exist!é“  z%Authentication token does not exist, zGThe authentication token is invalid, failed to access model {model_id}.é�  éô  ú/%s Client Error: %s, Request id: %s for url: %séX  ú0%s Server Error: %s, Request id: %s, for url: %sr   )
Ú
isinstanceÚreasonÚbytesr(   ÚUnicodeDecodeErrorr   Ústatus_coder,   r-   r   )r   ÚloggerÚcookiesÚmodel_idr/   Úhttp_error_msgr:   Ú
request_idr   r   r   Úhandle_http_responseZ   s:   ÿ


ÿÿ
rC   c                 C   s   | d t jkr	dS t| d ƒ‚)zøIf response error, raise exception

    Args:
        rsp (_type_): The server response

    Raises:
        RequestError: the response error message.

    Returns:
        bool: True if request is OK, otherwise raise `RequestError` exception.
    r   TÚMessage)r   r    r   r!   r   r   r   r/      s   Úhttp_responsec              
   C   sF   |  d¡tjkr
dS |d }td| › d|› d|d › d|d › d	�	ƒ‚)
aQ  If response error, raise exception

    Args:
        url (str): The request url
        rsp (HTTPResponse): The server response.
        http_response: the origin http response.

    Raises:
        RequestError: the http request error.

    Returns:
        bool: `True` if request is OK, otherwise raise `RequestError` exception.
    r   TÚ	RequestIdzUrl = z, Request id=z Code = z Message = rD   zC,                Please specify correct dataset_name and namespace.)r%   r   r    r   )r,   r"   rE   rB   r   r   r   Údatahub_raise_on_error‘   s   $ÿrG   c                 C   s  d}t | jtƒr z| j d¡}W n ty   | j d¡}Y nw | j}t| ƒ}d| jkr6d}d| j|f  n7d| jkr>d}n/d	| j  krId
k rVn nd| j||| jf }nd
| j  kradk rmn n
d| j||| jf }|r„| j}|j	dkr~d||j
f }t|| d�‚dS )zðAttempt to decode utf-8 first since some servers
    localize reason strings, for invalid utf-8, fall back
    to decoding with iso-8859-1.

    Args:
        rsp: The http response.

    Raises:
        HTTPError: The http error info.
    r   r$   r1   r2   z7The request resource(model or dataset) does not exist!,zurl: %s, reason: %sr3   z/Authentication token does not exist or invalid.r4   r5   r6   r7   r8   ÚPOSTz%s, body: %sr   N)r9   r:   r;   r(   r<   r   r=   r,   r   ÚmethodÚbodyr   )r"   rA   r:   rB   Úreqr   r   r   Úraise_for_http_status¨   s8   ÿ

ÿÿ
ürL   )T)#ÚloggingÚhttpr   Útypingr   ÚrequestsZrequests.exceptionsr   Úmodelscope.hub.constantsr   Úmodelscope.utils.loggerr   ÚWARNINGr>   Ú	Exceptionr	   r   r   r   r   r   r   r   r   ÚResponser   r#   r*   r.   ÚboolÚintrC   r/   rG   rL   r   r   r   r   Ú<module>   s<   
üü
ü%