o
    ưit                     @   s>   d dl mZ d dlmZmZmZ d dlmZ G dd dZdS )    )Union)DB_CONNECTION_ERROR_TYPESProxyErrorTypesProxyException)str_to_boolc                   @   s`   e Zd ZdZedefddZededefddZededefdd	Z	edefd
dZ
dS )PrismaDBExceptionHandlerz<
    Class to handle DB Exceptions or Connection Errors
    returnc                  C   s:   ddl m}  | dd}t|tr|S t|du rdS dS )zj
        Returns True if the request should be allowed to proceed despite the DB connection error
        r   )general_settingsZ allow_requests_on_db_unavailableFT)Zlitellm.proxy.proxy_serverr	   get
isinstanceboolr   )r	   Z!_allow_requests_on_db_unavailable r   Y/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/db/exception_handler.py&should_allow_request_on_db_unavailable   s   
z?PrismaDBExceptionHandler.should_allow_request_on_db_unavailableec                 C   sF   ddl }t| trdS t| |jjrdS t| tr!| jtjkr!dS dS )u   
        Returns True if the exception is from a database outage / connection error.
        Any PrismaError qualifies — the DB failed to serve the request.
        Used by allow_requests_on_db_unavailable logic and endpoint 503 responses.
        r   NTF)	prismar   r   errorsPrismaErrorr   typer   no_db_connection)r   r   r   r   r   is_database_connection_error    s   
z5PrismaDBExceptionHandler.is_database_connection_errorc                    s   ddl }t| trdS t| |jj|jjfrdS t| |jjr4t|   d}t	 fdd|D r4dS t| t
rA| jtjkrAdS dS )u?  
        Returns True only for transport/connectivity failures where a reconnect
        attempt makes sense (e.g. DB is unreachable, connection dropped).

        Use this for reconnect logic — data-layer errors like UniqueViolationError
        mean the DB IS reachable, so reconnecting would be pointless.
        r   NT)zcan't reach database serverzcannot reach database serverzcan't connectzcannot connectzconnection errorzconnection closedz	timed outtimeoutzconnection refusedznetwork is unreachablezno route to hostzbroken pipec                 3   s    | ]}| v V  qd S )Nr   ).0keyworderror_messager   r   	<genexpr>R   s    zGPrismaDBExceptionHandler.is_database_transport_error.<locals>.<genexpr>F)r   r   r   r   ZClientNotConnectedErrorZHTTPClientClosedErrorr   strloweranyr   r   r   r   )r   r   Zconnection_keywordsr   r   r   is_database_transport_error1   s   	
z4PrismaDBExceptionHandler.is_database_transport_errorc                 C   s   t | rt  rdS | )aK  
        Primary handler for `allow_requests_on_db_unavailable` flag. Decides whether to raise a DB Exception or not based on the flag.

        - If exception is a DB Connection Error, and `allow_requests_on_db_unavailable` is True,
            - Do not raise an exception, return None
        - Else, raise the exception
        N)r   r   r   )r   r   r   r   handle_db_exceptionX   s   
z,PrismaDBExceptionHandler.handle_db_exceptionN)__name__
__module____qualname____doc__staticmethodr   r   	Exceptionr   r    r!   r   r   r   r   r      s    &r   N)	typingr   Zlitellm.proxy._typesr   r   r   Zlitellm.secret_managers.mainr   r   r   r   r   r   <module>   s    