<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
B
    Y2                  @   st   d 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 ddlmZ G dd	 d	eZG d
d deZdS )z
raven.contrib.tornado
~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2012 by the Sentry Team, see AUTHORS for more details
:license: BSD, see LICENSE for more details
    )absolute_import)partial)ioloop)AsyncHTTPClient	HTTPError)r   )Clientc                   sN   e Zd ZdZ fddZdd ZdddZdd	d
Zdd ZdddZ	  Z
S )AsyncSentryClientz
    A mixin class that could be used along with request handlers to
    asynchronously send errors to sentry. The client also captures the
    information from the request handlers
    c                s$   | dd| _tt| j|| d S )Nvalidate_certT)popr	   superr   __init__)selfargskwargs)	__class__ O/opt/alt/python37/lib/python3.7/site-packages/raven/contrib/tornado/__init__.pyr      s    zAsyncSentryClient.__init__c             O   s@   |   sdS | j||}| jf d|ddi|}|d |fS )a	  
        Takes the same arguments as the super function in :py:class:`Client`
        and extracts the keyword argument callback which will be called on
        asynchronous sending of the request

        :return: a 32-length string identifying this event
        NcallbackZevent_id)
is_enabledZ	build_msgsendget)r   r   r   datafuturer   r   r   capture   s
    zAsyncSentryClient.captureNc             K   s   |  |}| j|||dS )zV
        Serializes the message and passes the payload onto ``send_encoded``.
        )auth_headerr   )encodeZsend_encoded)r   r   r   r   messager   r   r   r   /   s    
zAsyncSentryClient.sendc             C   s`   |d kri }| j  s.| |}| | d S | j||||d}tj |t	| j
|| |S )N)urlr   headersr   )stateZ
should_trydecodeZ_log_failed_submission_send_remoter   ZIOLoopcurrentZ
add_futurer   _handle_result)r   r   r   r   r   r   r   r   r   send_remote7   s    


zAsyncSentryClient.send_remotec          
   C   s   y|   W nr tk
rF } z| |}| ||| W d d }~X Y nD tk
r~ } z| |}| ||| W d d }~X Y nX | j  d S )N)resultr   r    Z_failed_send	Exceptionr   Zset_success)r   r   r   r   er   r   r   r#   F   s    

 z AsyncSentryClient._handle_resultc             C   s&   |dkri }t  j||d||| jdS )z
        Initialise a Tornado AsyncClient and send the reuqest to the sentry
        server. If the callback is a callable, it will be called with the
        response.
        NZPOST)methodbodyr   r	   )r   Zfetchr	   )r   r   r   r   r   r   r   r   r!   R   s
    
zAsyncSentryClient._send_remote)NN)NN)NN)__name__
__module____qualname____doc__r   r   r   r$   r#   r!   __classcell__r   r   )r   r   r      s   

r   c                   sr   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dddZ
dd Z fddZd fdd	Z  ZS )SentryMixina  
    A mixin class that extracts information from the Request in a Request
    Handler to capture and send to sentry. This mixin class is designed to be
    used along with `tornado.web.RequestHandler`

    .. code-block:: python
        :emphasize-lines: 6

        class MyRequestHandler(SentryMixin, tornado.web.RequestHandler):
            def get(self):
                try:
                    fail()
                except Exception as e:
                    self.captureException()


    While the above example would result in sequential execution, an example
    for asynchronous use would be

    .. code-block:: python
        :emphasize-lines: 6

        class MyRequestHandler(SentryMixin, tornado.web.RequestHandler):

            @tornado.web.asynchronous
            @tornado.gen.engine
            def get(self):
                # Do something and record a message in sentry
                response = yield tornado.gen.Task(
                    self.captureMessage, "Did something really important"
                )
                self.write("Your request to do something important is done")
                self.finish()


    The mixin assumes that the application will have an attribute called
    `sentry_client`, which should be an instance of
    :py:class:`AsyncSentryClient`. This can be changed by implementing your
    own get_sentry_client method on your request handler.
    c             C   s   | j jS )z
        Returns the sentry client configured in the application. If you need
        to change the behaviour to do something else to get the client, then
        subclass this method
        )ZapplicationZsentry_client)r   r   r   r   get_sentry_client   s    zSentryMixin.get_sentry_clientc          	   C   s<   d| j  | j j| j j| j j| j jddt| j jdiS )z
        Extracts the data required for 'sentry.interfaces.Http' from the
        current request being handled by the request handler

        :param return: A dictionary.
        requestCookieN)r   r(   r   Zquery_stringZcookiesr   )r1   Zfull_urlr(   r)   queryr   r   dict)r   r   r   r   get_sentry_data_from_request   s    z(SentryMixin.get_sentry_data_from_requestc             C   s6   y|   }W n tk
r    i S X dd|r.dndiiS )z
        Data for sentry.interfaces.User

        Default implementation only sends `is_authenticated` by checking if
        `tornado.web.RequestHandler.get_current_user` tests postitively for on
        Truth calue testing
        userZis_authenticatedTF)Zget_current_userr&   )r   r6   r   r   r   get_sentry_user_info   s    z SentryMixin.get_sentry_user_infoc             C   s   di iS )zf
        Subclass and implement this method if you need to send any extra
        information
        extrar   )r   r   r   r   get_sentry_extra_info   s    z!SentryMixin.get_sentry_extra_infoc             C   s2   i }| |   | |   | |   |S )N)updater5   r7   r9   )r   r   r   r   r   get_default_context   s
    zSentryMixin.get_default_contextNc             K   s`   |d kr|   }n.|   }t|tr0|| n||d d< |}|  }t||f d|i|S )Nr8   
extra_datar   )r;   
isinstancer4   r:   r0   getattr)r   Z	call_namer   r   Zdefault_contextZclientr   r   r   _capture   s    

zSentryMixin._capturec             K   s   | j dd|i|S )NcaptureExceptionexc_info)r@   )r?   )r   rA   r   r   r   r   r@      s    zSentryMixin.captureExceptionc             K   s   | j dd|i|S )NcaptureMessager   )rB   )r?   )r   r   r   r   r   r   rB      s    zSentryMixin.captureMessagec                sL   t t| |||}t|tr6|jdk s2|jdkr6|S | j|||fd |S )zvOverride implementation to report all exceptions to sentry.
        log_exception() is added in Tornado v3.1.
        i  iW  )rA   )r   r/   log_exceptionr=   WebHTTPErrorstatus_coder@   )r   typvaluetbrv)r   r   r   rC      s
    zSentryMixin.log_exception  c                sj   t tt| dr$tt| j|f|S tt| j|f|}d|  krLdkrbn n| j|dd |S dS )zOverride implementation to report all exceptions to sentry, even
        after self.flush() or self.finish() is called, for pre-v3.1 Tornado.
        rC   i  iW  rA   )rA   N)hasattrr   r/   
send_errorr@   r   )r   rE   r   rI   )r   r   r   rL      s    zSentryMixin.send_error)N)N)rJ   )r*   r+   r,   r-   r0   r5   r7   r9   r;   r?   r@   rB   rC   rL   r.   r   r   )r   r   r/   a   s   (


r/   N)r-   
__future__r   	functoolsr   Ztornador   Ztornado.httpclientr   r   Ztornado.webrD   Z
raven.baser   r   objectr/   r   r   r   r   <module>   s   N