o
    0 i%                     @   s  d dl Z d dlmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZ d dlmZ erNd dlmZ d d	lmZ d d
lmZ d dlmZmZ zd dlZd dlmZ W n eye   edw zd dlZejZejZ dZ!W n ey   d dl"Zej#Zej$Z dZ!Y nw dZ%e!re  d dl&m'Z% W d   n1 sw   Y  G dd deZ(G dd dZ)dZ*G dd deZ+dd Z,dd Z-dd Z.dd Z/d d! Z0d"d# Z1d$d% Z2dS )&    N)_check_minimum_versionIntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionparse_version)TYPE_CHECKING)Any)Dict)Optional)EventEventProcessor)__version__zFalcon not installedTF)_UNSETc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )FalconRequestExtractorc                 C      | j jS N)requestenvself r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/falcon.pyr   5      zFalconRequestExtractor.envc                 C   r   r   )r   cookiesr   r   r   r   r   9   r   zFalconRequestExtractor.cookiesc                 C      d S r   r   r   r   r   r   form=      zFalconRequestExtractor.formc                 C   r   r   r   r   r   r   r   filesA   r!   zFalconRequestExtractor.filesc                 C   s   |   }|dkr
dS d S )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r#   r   r   r   raw_dataE   s   zFalconRequestExtractor.raw_datac                 C   sB   d }t   | jj}W d    n1 sw   Y  |tur|S d S r   )r   r   Z_media_FALCON_UNSET)r   Zcached_mediar   r   r   jsonR   s   
	zFalconRequestExtractor.jsonN)	__name__
__module____qualname__r   r   r    r"   r$   r&   r   r   r   r   r   4   s    r   c                   @   s   e Zd ZdZdd ZdS )SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to Sentryc                 O   s<   t  t}|d u rd S t  }d|_|t|| d S )Nfalcon)
sentry_sdk
get_clientget_integrationFalconIntegrationZget_isolation_scope_nameZadd_event_processor_make_request_event_processor)r   reqrespargskwargsintegrationscoper   r   r   process_requesth   s   z&SentryFalconMiddleware.process_requestN)r'   r(   r)   __doc__r8   r   r   r   r   r*   e   s    r*   uri_templatepathc                   @   s4   e Zd ZdZde ZdZd
ddZedd Zd	S )r/   r+   z
auto.http. r;   c                 C   s"   |t vrtd|t f || _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrortransaction_style)r   r@   r   r   r   __init__|   s   
zFalconIntegration.__init__c                  C   s(   t t} tt|  t  t  t  d S r   )r   FALCON_VERSIONr   r/   _patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_once   s
   

zFalconIntegration.setup_onceN)r;   )	r'   r(   r)   
identifieroriginr@   rA   staticmethodrG   r   r   r   r   r/   v   s    

	r/   c                     s   t j  fdd} | t _d S )Nc                    sD   t  t}|d u r ||S t fddtjd}|||S )Nc                    s    | |S r   r   )ZenviZ
start_resp)original_wsgi_appr   r   r   <lambda>   s    zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)Zspan_origin)r,   r-   r.   r/   r   rI   )r   r   Zstart_responser6   Zsentry_wrappedrK   r   r   sentry_patched_wsgi_app   s   
z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)falcon_app_class__call__)rN   r   rM   r   rC      s   
rC   c                     s&   t j tt  fdd} | t _d S )Nc                    s   d  }}t   tdd |D }tdd |D }W d    n1 s$w   Y   | g|R  }|d u s9|d u r;|S t||rVt|t jdddd\}}tj||d |S )	Nc                 s   s    | ]
}t |tr|V  qd S r   )
isinstance	Exception.0argumentr   r   r   	<genexpr>   s    zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>c                 s   s     | ]}t |tjr|V  qd S r   )rQ   r+   ResponserS   r   r   r   rV      s    
r+   F)typeZhandled)Zclient_optionsZ	mechanism)hint)r   next_exception_leads_to_http_5xxr
   r,   r-   optionsZcapture_event)r   r4   exresponseZwas_handledeventrY   Zoriginal_handle_exceptionr   r   sentry_patched_handle_exception   s$   


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)rO   Z_handle_exceptionr	   r/   )ra   r   r`   r   rD      s   
rD   c                     s    t j 	d fdd	} | t _d S )NFc                    s@   |r | ||S t  t}|d urt g| pg  }  | |S r   )r,   r-   r.   r/   r*   )Z
middlewareZindependent_middlewareZasgir6   Zoriginal_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s   
zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NFF)falcon_helpersZprepare_middleware)rc   r   rb   r   rE      s   
rE   c                 C   sH   t | tjo| jp
dd}t | tjtjjf }|s|o#t p#t|S )Nr=   5)	rQ   r+   	HTTPErrorstatus
startswithZhttp_status
HTTPStatusFALCON3_has_http_5xx_status)r]   r^   Zis_server_errorZis_unhandled_errorr   r   r   r[      s   	r[   c                 C   s   | j dS )Nre   )rg   rh   )r^   r   r   r   rk      s   rk   c                 C   s.   |j |jd}|| | d< dt| i| d< d S )Nr:   ZtransactionsourceZtransaction_info)r;   r<   r   )r_   r@   r   Zname_for_styler   r   r    _set_transaction_name_and_source   s
   rm   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r   )rm   r@   r   r   Zextract_into_event)r_   rY   r6   r2   r   r   event_processor  s   
z6_make_request_event_processor.<locals>.event_processorr   )r2   r6   ro   r   rn   r   r1     s   	r1   )3r,   Zsentry_sdk.integrationsr   r   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.wsgir   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r	   r
   r   typingr   r   r   r   Zsentry_sdk._typesr   r   r+   r   rB   ImportErrorZfalcon.app_helpersZapp_helpersrd   ZApprO   rj   Zfalcon.api_helpersZapi_helpersZAPIr%   Zfalcon.requestr   r   r*   r>   r/   rC   rD   rE   r[   rk   rm   r1   r   r   r   r   <module>   sZ    1&
