
    Dj%                        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mZmZmZ d dlmZ er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Zd dlmZ 	 d dlZej8                  Zej<                  ZdZ dZ$e r e       5  d dl%m&Z$ ddd        G d de      Z' G d d      Z(dZ) G d de      Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1y# e$ r	  ed      w xY w# e$ r! d dl!ZejD                  ZejF                  ZdZ Y w xY w# 1 sw Y   {xY w)    N)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                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)FalconRequestExtractorc                 .    | j                   j                  S N)requestenvselfs    bC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sentry_sdk/integrations/falcon.pyr   zFalconRequestExtractor.env5   s    ||    c                 .    | j                   j                  S r   )r   cookiesr   s    r   r   zFalconRequestExtractor.cookies9   s    ||###r   c                      y r    r   s    r   formzFalconRequestExtractor.form=       r   c                      y r   r!   r   s    r   fileszFalconRequestExtractor.filesA   r#   r   c                 0    | j                         }|dkD  ryy )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r'   s     r   raw_datazFalconRequestExtractor.raw_dataE   s      ,,.A2r   c                     d }t               5  | j                  j                  }d d d        |t        ur|S y # 1 sw Y   xY wr   )r   r   _media_FALCON_UNSET)r   cached_medias     r   jsonzFalconRequestExtractor.jsonR   sH     (* 	/  <<..L	/ },	/ 	/s	   7A N)	__name__
__module____qualname__r   r   r"   r%   r(   r-   r!   r   r   r   r   4   s      $r   r   c                       e Zd ZdZd Zy)SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to Sentryc                     t        j                         j                  t              }|y t        j                         }d|_        |j                  t        ||             y )Nfalcon)
sentry_sdk
get_clientget_integrationFalconIntegrationget_isolation_scope_nameadd_event_processor_make_request_event_processor)r   reqrespargskwargsintegrationscopes          r   process_requestz&SentryFalconMiddleware.process_requesth   sP     ++-==>OP..0!!"?["QRr   N)r.   r/   r0   __doc__rC   r!   r   r   r2   r2   e   s    CSr   r2   uri_templatepathc                   6    e Zd ZdZde ZdZddZed        Zy)r8   r4   z
auto.http. c                 N    |t         vrt        d|dt         d      || _        y )Nz%Invalid value for transaction_style: z (must be in ))TRANSACTION_STYLE_VALUES
ValueErrortransaction_style)r   rN   s     r   __init__zFalconIntegration.__init__|   s.    $<<$&>@  "3r   c                      t        t              } | t        dj                  t                    | dk  rt        d      t	                t                t                y )NzUnparsable Falcon version: {})      zFalcon 1.4 or newer required.)r   FALCON_VERSIONr   format_patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versions    r   
setup_oncezFalconIntegration.setup_once   sP      /?>EEnUVVV>??!!#r   N)rF   )	r.   r/   r0   
identifieroriginrN   rO   staticmethodrY   r!   r   r   r8   r8   v   s2    J*&F3 $ $r   r8   c                  F    t         j                  fd} | t         _        y )Nc                      t        j                         j                  t              }|
  ||      S t	         fdt        j
                        } |||      S )Nc                      | |      S r   r!   )envi
start_resporiginal_wsgi_appr   s     r   <lambda>zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>   s    %6tT:%N r   )span_origin)r5   r6   r7   r8   r   r[   )r   r   start_responserA   sentry_wrappedrb   s   `    r   sentry_patched_wsgi_appz0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app   sW     ++-==>OP$T3??-N)00

 c>22r   )falcon_app_class__call__)rg   rb   s    @r   rU   rU      s    (113 !8r   c                  l    t         j                  t        t              fd       } | t         _        y )Nc                 Z   d x}}t               5  t        d |D              }t        d |D              }d d d         | g| }|||S t        ||      rGt        |t	        j
                         j                  ddd      \  }}t	        j                  ||       |S # 1 sw Y   mxY w)Nc              3   B   K   | ]  }t        |t              s|  y wr   )
isinstance	Exception.0arguments     r   	<genexpr>zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   s     W8z(I7VhWs   c              3   V   K   | ]!  }t        |t        j                        s| # y wr   )rm   r4   Responsero   s     r   rr   zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   s"      %Hfoo1Vs   ))r4   F)typehandled)client_options	mechanism)hint)r   next_exception_leads_to_http_5xxr
   r5   r6   optionscapture_event)r   r?   exresponsewas_handledeventry   original_handle_exceptions          r   sentry_patched_handle_exceptionz@_patch_handle_exception.<locals>.sentry_patched_handle_exception   s     X(* 	WtWWB )- H	 0<t<:) 'H5.)446>>#+>KE4
 $$U6-	 	s   %B!!B*)rh   _handle_exceptionr	   r8   )r   r   s    @r   rV   rV      s7     0 B B 13LM N< *I&r   c                  J    t         j                  	 dfd	} | t         _        y )Nc                     |r
 | ||      S t        j                         j                  t              }|t	               g| xs g z   }  | |      S r   )r5   r6   r7   r8   r2   )
middlewareindependent_middlewareasgirA   original_prepare_middlewares       r   !sentry_patched_prepare_middlewarezD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware   s^     .z;QSWXX ++-==>OP"023z7GRHJ +:7MNNr   )NFF)falcon_helpersprepare_middleware)r   r   s    @r   rW   rW      s&    "0"C"C =BO  )JN%r   c                    t        | t        j                        xr | j                  xs dj	                  d      }t        | t        j                  t        j
                  j                  f       }|xs |xr t         xs t        |      S )NrI   5)	rm   r4   	HTTPErrorstatus
startswithhttp_status
HTTPStatusFALCON3_has_http_5xx_status)r~   r   is_server_erroris_unhandled_errors       r   r{   r{      s     V%5%56 BIIO;W;W<O (
Vv11<<=  11 5+H5r   c                 8    | j                   j                  d      S )Nr   )r   r   )r   s    r   r   r      s    ??%%c**r   c                 b    |j                   |j                  d}||   | d<   dt        |   i| d<   y )NrE   transactionsourcetransaction_info)rF   rG   r   )r   rN   r   name_for_styles       r    _set_transaction_name_and_sourcer      sD      ,,N **;<E-!)+;<M+N OE
r   c                       fd}|S )Nc                     t        | j                         t               5  t              j	                  |        d d d        | S # 1 sw Y   | S xY wr   )r   rN   r   r   extract_into_event)r   ry   rA   r=   s     r   event_processorz6_make_request_event_processor.<locals>.event_processor  sO    (0M0MsS(* 	B"3'::5A	B 	B s   AAr!   )r=   rA   r   s   `` r   r<   r<   	  s     r   )2r5   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.wsgir   sentry_sdk.tracingr   sentry_sdk.utilsr   r	   r
   r   typingr   r   r   r   sentry_sdk._typesr   r   r4   r   rS   ImportErrorfalcon.app_helpersapp_helpersr   Apprh   r   falcon.api_helpersapi_helpersAPIr+   falcon.requestr   r   r2   rL   r8   rU   rV   rW   r{   r   r   r<   r!   r   r   <module>r      s(    = A = /  !7
/4''NzzG 
	$	& ;:;.- .bS S 4 $ $@8(#ILJ.&+
PY  /
-
../  ''NzzG; ;s*   
C	 C D	C#D ?D D