o
    Uhr                     @   s  d dl Z d dlmZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ e
eZG dd	 d	eZG d
d deZdededdfddZdededefddZdededefddZdededefddZdededefddZdededefddZdS )    N)HTTPExceptionRequest)JSONResponse)ProgrammingError)OperationalError)
get_logger)CUSTOM_LOGGERc                	       s8   e Zd ZdZd	dedededB ddf fddZ  ZS )
CustomHttpExceptionzFCustom HTTP Exception class to handle exceptions with custom messages.Nstatus_codedetailheadersreturnc                       t  j|||d d S N)r
   r   r   super__init__selfr
   r   r   	__class__ )/var/www/html/RetailAPI/app/exceptions.pyr         zCustomHttpException.__init__)N	__name__
__module____qualname____doc__intstrdictr   __classcell__r   r   r   r   r	          ,r	   c                	       s8   e Zd ZdZddedededB ddf fd	d
Z  ZS )SessionExceptionzSession expired exception.  Session expiredNr
   r   r   r   c                    r   r   r   r   r   r   r   r      r   zSessionException.__init__)r%   r&   Nr   r   r   r   r   r$      r#   r$   requestexcr   c                 C   sh   | j j}d| d| }t }| d| }t| t| t|t	s2t| t
| dS dS )zLog the error message.z[Error] z: 
N)urlpath	traceback
format_exclogerrorr   	error_log
isinstancer   stack_trace)r'   r(   url_patherror_messageerror_tracebackfull_error_messager   r   r   	log_error   s   



r7   c                 C   s&   |j }t| | tdd|jd|dS )z1HTTP Exception handler to return custom messages.   -1)statusmessage)r
   contentr   )r   r7   r   r   )r'   r(   r   r   r   r   http_exception_error_handler*   s   
r=   c                    sL   t |}t| | d|v rd|v rtddd|ddS tddd|ddS )	z"Handle ProgrammingError exception.Tablezdoesn't existr8   r9   zRequested resource not foundr:   r;   r/   r
   r<   zInternal server error)r    r7   r   r'   r(   r4   r   r   r   handle_programming_error2   s   


rB   c                    s$   t | | tdddt|ddS )zHandle generic exception.r8   r9   zAn error occurredr?   r@   )r7   r   r    )r'   r(   r   r   r    handle_generic_exception_handlerA   s   
rC   c                        t |}t| | td|dS )z"Handle OperationalError exception.  r
   r   r    r7   r   rA   r   r   r    handel_operational_error_handlerJ      
rH   c                    rD   )z Handle AttributeError exception.rE   rF   rG   rA   r   r   r   handel_attribute_error_handlerP   rI   rJ   )r,   fastapir   r   fastapi.responsesr   sqlalchemy.excr   r   app.logr   app.utils.log_utilsr   r   r.   r	   r$   	Exceptionr7   r=   rB   rC   rH   AttributeErrorrJ   r   r   r   r   <module>   s     	