o
    @Tj                     @   s^   d Z ddlZddlZddl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dS )z!Global error handling middleware.    N)Request)JSONResponse)BaseHTTPMiddlewarec                   @   s   e Zd ZdZdefddZdS )ErrorHandlingMiddlewarezFCatch unhandled exceptions and return structured JSON error responses.requestc              
      s   t  jd d }t }z#||I d H }t | }td|j|jj|j	|| ||j
d< |W S  tyX   t | }td|j|jj|| tdd|dd|id Y S w )	N   z%s %s -> %s (%.3fs) [%s]zX-Request-IDz#Unhandled error: %s %s (%.3fs) [%s]i  u+   서버 내부 오류가 발생했습니다.)detail
request_id)status_codecontentheaders)uuiduuid4hextimeloggerinfomethodurlpathr
   r   	Exception	exceptionr   )selfr   	call_nextr	   startresponseelapsed r   ./home/ubuntu/htdocs/ootd-api/app/middleware.pydispatch   sB   

z ErrorHandlingMiddleware.dispatchN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   )r#   loggingr   r   fastapir   fastapi.responsesr   starlette.middleware.baser   	getLoggerr    r   r   r   r   r   r   <module>   s    
