<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from functools import partial <EOL> from tornado import ioloop <EOL> from tornado . httpclient import AsyncHTTPClient , HTTPError <EOL> from tornado . web import HTTPError as WebHTTPError <EOL> from raven . base import Client <EOL> class AsyncSentryClient ( Client ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . validate_cert = kwargs . pop ( '<STR_LIT>' , True ) <EOL> super ( AsyncSentryClient , self ) . __init__ ( * args , ** kwargs ) <EOL> def capture ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_enabled ( ) : <EOL> return <EOL> data = self . build_msg ( * args , ** kwargs ) <EOL> future = self . send ( callback = kwargs . get ( '<STR_LIT>' , None ) , ** data ) <EOL> return ( data [ '<STR_LIT>' ] , future ) <EOL> def send ( self , auth_header = None , callback = None , ** data ) : <EOL> """<STR_LIT>""" <EOL> message = self . encode ( data ) <EOL> return self . send_encoded ( message , auth_header = auth_header , callback = callback ) <EOL> def send_remote ( self , url , data , headers = None , callback = None ) : <EOL> if headers is None : <EOL> headers = { } <EOL> if not self . state . should_try ( ) : <EOL> data = self . decode ( data ) <EOL> self . _log_failed_submission ( data ) <EOL> return <EOL> future = self . _send_remote ( <EOL> url = url , data = data , headers = headers , callback = callback <EOL> ) <EOL> ioloop . IOLoop . current ( ) . add_future ( future , partial ( self . _handle_result , url , data ) ) <EOL> return future <EOL> def _handle_result ( self , url , data , future ) : <EOL> try : <EOL> future . result ( ) <EOL> except HTTPError as e : <EOL> data = self . decode ( data ) <EOL> self . _failed_send ( e , url , data ) <EOL> except Exception as e : <EOL> data = self . decode ( data ) <EOL> self . _failed_send ( e , url , data ) <EOL> else : <EOL> self . state . set_success ( ) <EOL> def _send_remote ( self , url , data , headers = None , callback = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { } <EOL> return AsyncHTTPClient ( ) . fetch ( <EOL> url , callback , method = "<STR_LIT:POST>" , body = data , headers = headers , <EOL> validate_cert = self . validate_cert <EOL> ) <EOL> class SentryMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_sentry_client ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . sentry_client <EOL> def get_sentry_data_from_request ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:url>' : self . request . full_url ( ) , <EOL> '<STR_LIT>' : self . request . method , <EOL> '<STR_LIT:data>' : self . request . body , <EOL> '<STR_LIT>' : self . request . query , <EOL> '<STR_LIT>' : self . request . headers . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : dict ( self . request . headers ) , <EOL> } <EOL> } <EOL> def get_sentry_user_info ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = self . get_current_user ( ) <EOL> except Exception : <EOL> return { } <EOL> return { <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT>' : True if user else False <EOL> } <EOL> } <EOL> def get_sentry_extra_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : { <EOL> } <EOL> } <EOL> def get_default_context ( self ) : <EOL> data = { } <EOL> data . update ( self . get_sentry_data_from_request ( ) ) <EOL> data . update ( self . get_sentry_user_info ( ) ) <EOL> data . update ( self . get_sentry_extra_info ( ) ) <EOL> return data <EOL> def _capture ( self , call_name , data = None , ** kwargs ) : <EOL> if data is None : <EOL> data = self . get_default_context ( ) <EOL> else : <EOL> default_context = self . get_default_context ( ) <EOL> if isinstance ( data , dict ) : <EOL> default_context . update ( data ) <EOL> else : <EOL> default_context [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data <EOL> data = default_context <EOL> client = self . get_sentry_client ( ) <EOL> return getattr ( client , call_name ) ( data = data , ** kwargs ) <EOL> def captureException ( self , exc_info = None , ** kwargs ) : <EOL> return self . _capture ( '<STR_LIT>' , exc_info = exc_info , ** kwargs ) <EOL> def captureMessage ( self , message , ** kwargs ) : <EOL> return self . _capture ( '<STR_LIT>' , message = message , ** kwargs ) <EOL> def log_exception ( self , typ , value , tb ) : <EOL> """<STR_LIT>""" <EOL> rv = super ( SentryMixin , self ) . log_exception ( typ , value , tb ) <EOL> if isinstance ( value , WebHTTPError ) and ( value . status_code < <NUM_LIT> or value . status_code > <NUM_LIT> ) : <EOL> return rv <EOL> self . captureException ( exc_info = ( typ , value , tb ) ) <EOL> return rv <EOL> def send_error ( self , status_code = <NUM_LIT> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( super ( SentryMixin , self ) , '<STR_LIT>' ) : <EOL> return super ( SentryMixin , self ) . send_error ( status_code , ** kwargs ) <EOL> else : <EOL> rv = super ( SentryMixin , self ) . send_error ( status_code , ** kwargs ) <EOL> if <NUM_LIT> <= status_code <= <NUM_LIT> : <EOL> self . captureException ( exc_info = kwargs . get ( '<STR_LIT>' ) ) <EOL> return rv </s>
