o
    h,%                     @   s  d dl mZ d dlm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mZmZ d dlmZ erPd d	lmZ d d
lmZ d dlmZ d dlmZ zd dlZd dlmZ W n eyg   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 G dd deZ%G dd de&Z'dZ(G dd deZ)dd Z*dd Z+dd Z,dd  Z-d!d" Z.d#d$ Z/d%d& Z0dS )'    )absolute_import)Hub)IntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exceptionparse_version)TYPE_CHECKING)Any)Dict)Optional)EventProcessorN)__version__zFalcon not installedTFc                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zer dd Z	dS dd Z	dS )FalconRequestExtractorc                 C      | j jS N)requestenvself r   a/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/falcon.pyr   0      zFalconRequestExtractor.envc                 C   r   r   )r   cookiesr   r   r   r   r   4   r   zFalconRequestExtractor.cookiesc                 C      d S r   r   r   r   r   r   form8      zFalconRequestExtractor.formc                 C   r   r   r   r   r   r   r   files<   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_data@   s   zFalconRequestExtractor.raw_datac                 C   s$   z| j jW S  tjjy   Y d S w r   )r   mediafalconerrorsHTTPBadRequestr   r   r   r   jsonO   s
   
zFalconRequestExtractor.jsonc                 C   s*   z| j jW S  tjjy   | j j Y S w r   )r   r#   r$   r%   r&   _mediar   r   r   r   r'   X   s
   
N)
__name__
__module____qualname__r   r   r   r    r"   FALCON3r'   r   r   r   r   r   /   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 j}|t}|d u rd S | }d|_|t|| W d    d S 1 s)w   Y  d S )Nr$   )r   currentget_integrationFalconIntegrationconfigure_scope_nameadd_event_processor_make_request_event_processor)r   reqrespargskwargshubintegrationscoper   r   r   process_requestg   s   

"z&SentryFalconMiddleware.process_requestN)r)   r*   r+   __doc__r<   r   r   r   r   r-   d   s    r-   uri_templatepathc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
r0   r$    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   rD   r   r   r   __init__{   s   
zFalconIntegration.__init__c                  C   sD   t t} | d u rtdt| dk rtdt  t  t  d S )NzUnparsable Falcon version: {})      zFalcon 1.4 or newer required.)r   FALCON_VERSIONr   format_patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_once   s   
zFalconIntegration.setup_onceN)r?   )r)   r*   r+   
identifierrD   rE   staticmethodrN   r   r   r   r   r0   v   s    
	r0   c                        t j  fdd} | t _d S )Nc                    s@   t j}|t}|d u r ||S t fdd}|||S )Nc                    s    | |S r   r   )envi
start_resp)original_wsgi_appr   r   r   <lambda>   s    zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)r   r.   r/   r0   r   )r   r   start_responser9   r:   sentry_wrappedrT   r   r   sentry_patched_wsgi_app   s   

z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)falcon_app_class__call__)rY   r   rX   r   rJ      s   
rJ   c                     rQ   )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j}|t}|d urct||rc|j}t||j	dddd\}}	|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   )r\   r$   Responser^   r   r   r   ra      s    
r$   F)typehandled)client_options	mechanism)hint)r	   nextr   r.   r/   r0   _exception_leads_to_http_5xxclientr
   optionscapture_event)
r   r7   exresponsewas_handledr9   r:   rj   eventrg   original_handle_exceptionr   r   sentry_patched_handle_exception   s*   


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)rZ   _handle_exception)rs   r   rq   r   rK      s   
$rK   c                     s    t j 	d fdd	} | t _d S )NFc                    sB   |r | ||S t j}|t}|d urt g| pg  }  | |S r   )r   r.   r/   r0   r-   )
middlewareindependent_middlewareasgir9   r:   original_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s   

zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NFF)falcon_helpersprepare_middleware)rz   r   rx   r   rL      s   
rL   c                 C   sH   t | tjo| jp
dd}t | tjtjjf }|s|o#t p#t|S )NrA   5)	r\   r$   	HTTPErrorstatus
startswithhttp_status
HTTPStatusr,   _has_http_5xx_status)rm   rn   is_server_erroris_unhandled_errorr   r   r   ri      s   	ri   c                 C   s   | j dS )Nr}   )r   r   )rn   r   r   r   r      s   r   c                 C   s.   |j |jd}|| | d< dt| i| d< d S )Nr>   transactionsourcetransaction_info)r?   r@   r   )rp   rD   r   name_for_styler   r   r    _set_transaction_name_and_source  s
   r   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r   )r   rD   r	   r   extract_into_event)rp   rg   r:   r5   r   r   event_processor  s   
z6_make_request_event_processor.<locals>.event_processorr   )r5   r:   r   r   r   r   r4     s   	r4   )1
__future__r   sentry_sdk.hubr   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.wsgir   sentry_sdk.tracingr   sentry_sdk.utilsr	   r
   r   sentry_sdk._typesr   typingr   r   r   r   r$   r   rH   ImportErrorfalcon.app_helpersapp_helpersr{   ApprZ   r,   falcon.api_helpersapi_helpersAPIr   objectr-   rB   r0   rJ   rK   rL   ri   r   r   r4   r   r   r   r   <module>   sR    5+
