a
    xd                     @   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 d dlmZ erd 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Zd dlmZ W n ey   edY n0 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S )#    )absolute_import)Hub)IntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exception)MYPY)Any)Dict)Optional)EventProcessorN)__version__zFalcon not installedc                   @   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   s   | j jS N)requestenvself r   V/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/falcon.pyr       s    zFalconRequestExtractor.envc                 C   s   | j jS r   )r   cookiesr   r   r   r   r   $   s    zFalconRequestExtractor.cookiesc                 C   s   d S r   r   r   r   r   r   form(   s    zFalconRequestExtractor.formc                 C   s   d S r   r   r   r   r   r   files,   s    zFalconRequestExtractor.filesc                 C   s   |   }|dkrdS d S d S )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r   r   r   r   raw_data0   s    zFalconRequestExtractor.raw_datac                 C   s.   z
| j jW S  tjjy(   | j j Y S 0 d S r   )r   ZmediafalconerrorsZHTTPBadRequestZ_mediar   r   r   r   json=   s    
zFalconRequestExtractor.jsonN)	__name__
__module____qualname__r   r   r   r   r   r    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    n1 sP0    Y  d S )Nr   )r   currentget_integrationFalconIntegrationZconfigure_scope_nameZadd_event_processor_make_request_event_processor)r   reqrespargskwargshubintegrationZscoper   r   r   process_requestL   s    

z&SentryFalconMiddleware.process_requestN)r!   r"   r#   __doc__r0   r   r   r   r   r$   I   s   r$   uri_templatepathc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
r'   r    r3   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   r8   r   r   r   __init__`   s    zFalconIntegration.__init__c               	   C   sd   zt tttd} W n$ ttfy<   tdtY n0 | dk rNtdt	  t
  t  d S )N.zUnparsable Falcon version: {})      zFalcon 1.4 or newer required.)tuplemapintFALCON_VERSIONsplitr7   	TypeErrorr   format_patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_oncei   s    zFalconIntegration.setup_onceN)r3   )r!   r"   r#   
identifierr8   r9   staticmethodrH   r   r   r   r   r'   [   s
   
	r'   c                     s    t jj  fdd} | t j_d S )Nc                    s@   t j}|t}|d u r$ ||S t fdd}|||S )Nc                    s    | |S r   r   )ZenviZ
start_resp)original_wsgi_appr   r   r   <lambda>       zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)r   r%   r&   r'   r   )r   r   Zstart_responser.   r/   Zsentry_wrappedrK   r   r   sentry_patched_wsgi_app}   s    
z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)r   API__call__)rO   r   rN   r   rD   y   s    rD   c                     s    t jj  fdd} | t j_d S )Nc           	         s   t |d tr|d }n|d } | g|R  }tj}|t}|d ur~t|r~|j}t||j	dddd\}}|j
||d |S )Nr      r   F)typeZhandled)Zclient_optionsZ	mechanism)hint)
isinstance	Exceptionr   r%   r&   r'   _exception_leads_to_http_5xxclientr
   optionsZcapture_event)	r   r,   exZwas_handledr.   r/   rX   eventrT   Zoriginal_handle_exceptionr   r   sentry_patched_handle_exception   s    


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)r   rP   Z_handle_exception)r]   r   r\   r   rE      s    rE   c                     s"   t jj d fdd	} | t j_d S )NFc                    s2   t j}|t}|d ur(t g| p$g  }  | |S r   )r   r%   r&   r'   r$   )Z
middlewareZindependent_middlewarer.   r/   Zoriginal_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s
    
zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NF)r   Zapi_helpersZprepare_middleware)r_   r   r^   r   rF      s     
rF   c                 C   s:   t | tjo| jpdd}t | tjtjjf }|p8|S )Nr5   5)rU   r   	HTTPErrorstatus
startswithZhttp_status
HTTPStatus)rZ   Zis_server_errorZis_unhandled_errorr   r   r   rW      s    rW   c                 C   s.   |j |jd}|| | d< dt| i| d< d S )Nr2   ZtransactionsourceZtransaction_info)r3   r4   r   )r[   r8   r   Zname_for_styler   r   r    _set_transaction_name_and_source   s
    rf   c                    s    fdd}|S )Nc                    sF   t |  j t  t|  W d    n1 s80    Y  | S r   )rf   r8   r	   r   Zextract_into_event)r[   rT   r/   r*   r   r   event_processor   s    ,z6_make_request_event_processor.<locals>.event_processorr   )r*   r/   rh   r   rg   r   r)      s    	r)   )'
__future__r   Zsentry_sdk.hubr   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.wsgir   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr	   r
   Zsentry_sdk._typesr   typingr   r   r   r   r   Zfalcon.api_helpersr   r@   ImportErrorr   objectr$   r6   r'   rD   rE   rF   rW   rf   r)   r   r   r   r   <module>   s8   *#
