Ejemplo n.º 1
0
    def test_scrub_kwargs(self, send_payload):

        def _raise(**kwargs):
            raise Exception()

        try:
            _raise(password='******', clear='text')
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertNotIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        keywords = payload['data']['body']['trace']['frames'][-1]['keywordspec']

        self.assertEqual(2, len(payload['data']['body']['trace']['frames'][-1]['locals'][keywords]))
        self.assertIn('password', payload['data']['body']['trace']['frames'][-1]['locals'][keywords])
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals'][keywords]['password'], '\*+')
        self.assertIn('clear', payload['data']['body']['trace']['frames'][-1]['locals'][keywords])
        self.assertEqual('text', payload['data']['body']['trace']['frames'][-1]['locals'][keywords]['clear'])
Ejemplo n.º 2
0
 def execute_wrapper(self, query_str, query_params=None):
     cursor = self.conn.cursor()
     try:
         cursor.execute(query_str, query_params)
     except MySQLError:
         rollbar.report_exc_info()
     return cursor
Ejemplo n.º 3
0
def get_user_by_email(email):
    """Gets a User by `email`
    Returns None if not found
    """
    from htk.apps.accounts.models import UserEmail

    if is_valid_email(email):
        # check for confirmed email addresses
        user_emails = UserEmail.objects.filter(email__iexact=email, is_confirmed=True)
        num_results = user_emails.count()
        if num_results == 1:
            user = user_emails[0].user
        elif num_results > 1:
            # there should only be one User with this email...
            # if there are more, we have a data error!
            raise NonUniqueEmail(email)
        else:
            # num_results == 0, so check UserModel for active users with email
            UserModel = get_user_model()
            try:
                user = UserModel.objects.get(email__iexact=email, is_active=True)
            except UserModel.MultipleObjectsReturned:
                user = None
                request = get_current_request()
                rollbar.report_exc_info()
                raise NonUniqueEmail(email)
            except UserModel.DoesNotExist:
                # also check newly registered accounts
                # if not user.is_active, handling will get passed downstream
                user = get_incomplete_signup_user_by_email(email)
    else:
        user = None
    return user
Ejemplo n.º 4
0
 def update_locale_info_by_ip_from_request(self, request):
     """Update user info by IP Address
     
     Store last_login_ip only when logging in
     Store country resolved from IP
     Store timezone resolved from IP
     
     Caller: api.auth.decorators.register_or_login_user
     
     Unknown whether this code throws an exception, but catch it upstream if any
     """
     try:
         ip = extract_request_ip(request)
         if ip and self.last_login_ip != ip:
             self.last_login_ip = ip
             try:
                 from htk.lib.geoip.utils import get_country_code_by_ip
                 from htk.lib.geoip.utils import get_timezone_by_ip
                 detected_country = get_country_code_by_ip(ip) or ''
                 detected_timezone = get_timezone_by_ip(ip) or ''
                 self.detected_country = detected_country
                 self.detected_timezone = detected_timezone
             except:
                 # couldn't find geoip records for ip, just be quiet for now
                 pass
             finally:
                 self.save()
     except:
         # error extracting IP or saving
         rollbar.report_exc_info(request=request)
    def crawl_by_url(cls, competition: Competition, url: str) -> int:
        count = 0
        logger = CrawlerLogger.get_logger_for_class(cls)

        logger.log("crawl {}".format(competition.__str__()))
        try:
            tables = cls.get_tables(competition.get_league_url())

            for table in tables:
                for row in table.findAll('tr'):
                    try:
                        if FSRLeagueParser.parse_row(row, competition):
                            count = count + 1
                    except Exception as e:
                        logger.error(e)
                        rollbar.report_exc_info(sys.exc_info())

        except Exception as e:
            logger.error(e)
            rollbar.report_exc_info(sys.exc_info())

        # TODO: statistics
        # if lock:
        #     with lock:
        #         self.statistics['teams'] += count
        # else:
        #     self.statistics['teams'] += count

        return count
Ejemplo n.º 6
0
    def test_serialize_and_send_payload(self, post=None):
        invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='
        invalid = base64.b64decode(invalid_b64)

        def _raise():
            # Make sure that the _invalid local variable makes its
            # way into the payload even if its value cannot be serialized
            # properly.
            _invalid = invalid

            # Make sure the Password field gets scrubbed even though its
            # original value could not be serialized properly.
            Password = invalid

            password = '******'
            raise Exception('bug bug')

        try:
            _raise()
        except:
            rollbar.report_exc_info()

        self.assertEqual(post.called, True)
        payload_data = post.call_args[1]['data']
        self.assertIsInstance(payload_data, str)
        self.assertIn('bug bug', payload_data)

        try:
            json.loads(post.call_args[1]['data'])
        except:
            self.assertTrue(False)
Ejemplo n.º 7
0
def upload(url, root, env=None, **kwargs):
    try:
        if not root:
            root = os.getcwd()
        args = dict(commit='', branch='', job='')
        args.update(kwargs)
        assert args.get('branch') not in ('', None), "branch is required"
        assert args.get('commit') not in ('', None), "commit hash is required"
        assert any((args.get('job'),
                   (args.get('build') and args.get('service') == 'circleci'),
                   args.get('token'))), "missing token or other required argument(s)"

        reports = build_reports(root)

        if env:
            reports = "\n<<<<<< ENV\n".join(("\n".join(["%s=%s" % (k, os.getenv(k, '')) for k in env]), reports))

        kwargs['package'] = "codecov-v%s" % VERSION

        url = "%s/upload/v2?%s" % (url, urlencode(dict([(k, v.strip()) for k, v in kwargs.items() if v is not None])))

        result = requests.post(url, data=reports)
        if result.status_code != 200:
            sys.stdout.write(result.text)
        result.raise_for_status()
        return result.json()

    except AssertionError as e:
        rollbar.report_exc_info()
        return dict(message=str(e), uploaded=False, coverage=0)
Ejemplo n.º 8
0
def division_lookup():
    try:
        if request.json is None and request.method == 'POST':
            abort(400, "Must provide JSON (did you set Content-type?)")
        elif request.method == 'POST':
            args = request.json
        else:
            args = request.args

        if 'latitude' not in args:
            abort(400, "Most provide latitude and longitude")
        if 'longitude' not in args:
            abort(400, "Most provide latitude and longitude")

        conn = psycopg2.connect(host=dbcreds.HOSTNAME, database=dbcreds.DATABASE,
                                user=dbcreds.USERNAME, password=dbcreds.PASSWORD)
        cursor = conn.cursor()

        cursor.execute(QUERY_FORMAT.format(latitude=float(args['latitude']),
                                           longitude=float(args['longitude'])))

        result = cursor.fetchone()
        if result is None:
            name = None
        else:
            name = result[0].lower().translate(None, " -'")
        return jsonify({'division': name})
    except:
        rollbar.report_exc_info()
        raise
Ejemplo n.º 9
0
    def emit(self, record):
        if record.exc_info:
            rollbar.report_exc_info(record.exc_info)
        else:
            request = None

            rollbar.report_message(record.msg, record.levelname, request=request)
Ejemplo n.º 10
0
def api_call(server, type, data=None):
    api = get_api(server.address)

    try:
        if data:
            result = api.call(type, data)
        else:
            result = api.call(type)
    except Exception:
        rollbar.report_exc_info(
            extra_data={
                'server_id': server.id,
                'type': type,
                'data': data
            }
        )

        return None

    if not result or result.get('result') == API_CALL_RESULTS['exception']:
        extra_data = {
            'server_id': server.id,
            'data': data
        }

        if result:
            extra_data['message'] = result.get('message')
        else:
            extra_data['message'] = 'No result!'

        rollbar.report_message('Exception while calling server API', level='error',
                               extra_data=extra_data)
        return None

    return result
Ejemplo n.º 11
0
def application(environ, start_response):
    try:
        application = bottle.app()
        application.catchall = False
        application = JsonApiMiddleware(application)
        return application(environ, start_response)

    except:
        rollbar.report_exc_info(sys.exc_info(), webob.Request(environ))

        # Bare bones 500 handler
        content = b""
        if environ.get("JSON"):
            content = '{"__status_code__": 500}'
            content_type = "application/json; charset=UTF-8"
        else:
            dirname = os.path.dirname(__file__)
            five_hundred_path = os.path.join(dirname, "app/html/five_hundred.html")
            with open(five_hundred_path, "r", encoding="utf-8") as f:
                content = f.read()
            content_type = "text/html; charset=UTF-8"
        content = content.encode("utf-8")
        start_response(
            "500 Internal Server Error",
            [("Content-Type", content_type), ("Content-Length", str(len(content)))],
            sys.exc_info(),
        )
        environ["wsgi.errors"] = content
        return [content]
Ejemplo n.º 12
0
    def test_scrub_locals(self, send_payload):
        invalid_b64 = b'CuX2JKuXuLVtJ6l1s7DeeQ=='
        invalid = base64.b64decode(invalid_b64)

        def _raise():
            # Make sure that the _invalid local variable makes its
            # way into the payload even if its value cannot be serialized
            # properly.
            _invalid = invalid

            # Make sure the Password field gets scrubbed even though its
            # original value could not be serialized properly.
            Password = invalid

            password = '******'
            raise Exception((_invalid, Password, password))

        try:
            _raise()
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['password'], '\*+')
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals']['Password'], '\*+')
        self.assertIn('_invalid', payload['data']['body']['trace']['frames'][-1]['locals'])

        binary_type_name = 'str' if python_major_version() < 3 else 'bytes'
        undecodable_message = '<Undecodable type:(%s) base64:(%s)>' % (binary_type_name, base64.b64encode(invalid).decode('ascii'))
        self.assertEqual(undecodable_message, payload['data']['body']['trace']['frames'][-1]['locals']['_invalid'])
Ejemplo n.º 13
0
    def __call__(self, value):
        client = kickbox.Client(settings.KICKBOX_API_KEY)
        kickbox_client = client.kickbox()

        try:
            response = kickbox_client.verify(value)
        except ClientError as e:
            if e.code == 403 and e.message == 'Insufficient balance':
                rollbar.report_exc_info(sys.exc_info(), extra_data={'API': 'Kickbox', 'message': e.message})
                # send notification
                notification = KickboxAPIInsuffinetBalanceNotification()
                EmailAdminsNotificationSender(notification=notification).notify()
                # and raise ValidationError to mark data as not valid
                raise InsufficientCreditsError('Kickbox: insufficient balance')
            raise ValidationError('Kickbox: %s' % e.message)
        except Exception as e:
            raise ValidationError('Kickbox: %s' % e.message)
        else:
            if response.body['result'] in ('risky', 'undeliverable'):
                raise ValidationError('Kickbox: email risky or undeliverable')
            elif response.body['role']:
                raise ValidationError('Kickbox: email is a role email')
            elif response.body['free']:
                raise ValidationError('Kickbox: email address uses free email service')
            elif response.body['disposable']:
                raise ValidationError('Kickbox: email address uses a disposable domain')
            elif response.body['result'] == 'unknown':
                reason = response.body['reason']
                if reason == 'no_connect':
                    raise ValidationError('Kickbox: unable to connect to the SMTP server')
                else:
                    raise ValidationError('Kickbox: declined for reason "%s"' % reason)

        return True
Ejemplo n.º 14
0
    def get_story_list(self, batch):
        """ get a list of stories corresponding to a list of hashes """
        req_str = self.nb_endpoint + '/reader/starred_stories?'
        for a_hash in batch:
            req_str += 'h=' + a_hash + '&'
        stories = {}
	stories_req = requests.Request('GET', req_str, cookies=self.cookies)
        try:
            stories = self.request_with_backoff(stories_req)
        except requests.exceptions.ConnectionError as e:
            rollbar.report_exc_info()
            msg = 'Failed to get stories'
            logger.error(msg)
            logger.debug('Request string: %s', req_str)
            logger.error(e)
            statsd.event(msg, e.message, alert_type='error')
            logger.debug(stories.text)
        statsd.increment('nb.http_requests.get')
        story_list = []
        try:
            story_list = json.loads(stories.text)['stories']
        except ValueError as e:
            rollbar.report_exc_info()
            msg = 'Failed to parse stories response'
            logger.error(msg)
            logger.error(e)
            statsd.event(msg, e.message, alert_type='error')
            logger.debug(stories.text)
        return story_list
def send_new_opportunity_email_to_sellers(brief_json, brief_url):
    to_email_addresses = []
    if brief_json.get('sellerEmail'):
        to_email_addresses.append(brief_json['sellerEmail'])
    if brief_json.get('sellerEmailList'):
        to_email_addresses += brief_json['sellerEmailList']

    if to_email_addresses:
        email_body = render_template(
            'emails/seller_new_opportunity.html',
            brief=brief_json,
            brief_url=brief_url
        )

        for to_email_address in to_email_addresses:  # Send emails individually rather than sending to a list of emails

            try:
                send_email(
                    to_email_address,
                    email_body,
                    current_app.config['SELLER_NEW_OPPORTUNITY_EMAIL_SUBJECT'],
                    current_app.config['DM_GENERIC_NOREPLY_EMAIL'],
                    current_app.config['DM_GENERIC_SUPPORT_NAME'],
                )
            except EmailError as e:
                rollbar.report_exc_info()
                current_app.logger.error(
                    'seller new opportunity email failed to send. '
                    'error {error}',
                    extra={
                        'error': six.text_type(e), })
                abort(503, response='Failed to send seller new opportunity email.')
Ejemplo n.º 16
0
def create_campaign(client, recipients, settings=None):
    if settings is None:
        settings = {}

    settings.update({
        'from_name': 'The Marketplace team',
        'reply_to': current_app.config.get('GENERIC_CONTACT_EMAIL')
    })

    try:
        response = client.campaigns.create(
            data={
                'type': 'regular',
                'recipients': recipients,
                'settings': settings
            }
        )

        current_app.logger.info('Mailchimp campaign {} created with list {}'
                                .format(response['id'], recipients['list_id']))
        return response
    except RequestException as e:
        current_app.logger.error(
            'A Mailchimp API error occurred while creating a campaign, aborting: {} {}'.format(e, e.response))
        rollbar.report_exc_info()
        raise e
Ejemplo n.º 17
0
def handle_event(event):
    """Processes a validated skill request from Amazon Alexa

    Returns a payload if applicable, else None
    """
    event_handler = get_event_handler(event)
    if event_handler:
        try:
            payload = event_handler(event)
        except:
            payload = {
                'version' : '1.0',
                'response' : {
                    'outputSpeech' : {
                        'type' : 'SSML',
                        'ssml' : """<speak>Oops, I couldn't process that.</speak>""",
                    }
                },
            }
            rollbar.report_exc_info(extra_data={
                'event' : event,
                'event_handler' : event_handler.__name__,
            })
    else:
        payload = None
    return payload
Ejemplo n.º 18
0
    def test_long_list_arg_val(self, send_payload):

        def _raise(large):
            raise Exception()

        try:
            xlarge = ['hi' for _ in range(30)]
            # NOTE(cory): We copy the list here so that the local variables from
            # this frame are not referenced directly by the frame from _raise()
            # call above. If we didn't copy this list, Rollbar would report a
            # circular reference for the args on _raise().
            _raise([str(x) for x in xlarge])
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('args', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('kwargs', payload['data']['body']['trace']['frames'][-1])

        self.assertEqual(1, len(payload['data']['body']['trace']['frames'][-1]['args']))

        self.assertTrue(
            ("['hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', ...]" ==
                payload['data']['body']['trace']['frames'][-1]['args'][0])

            or

            ("['hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', ...]" ==
                    payload['data']['body']['trace']['frames'][0]['locals']['xlarge']))
Ejemplo n.º 19
0
    def send_activation_email(self, domain=None, resend=False, template=None, subject=None, sender=None):
        """Sends an activation email
        """
        domain = domain or htk_setting('HTK_DEFAULT_EMAIL_SENDING_DOMAIN')
        self._reset_activation_key(resend=resend)

        try:
            should_send_activation_email = True

            if htk_setting('HTK_ITERABLE_ENABLED'):
                from htk.lib.iterable.utils import get_iterable_api_client
                from htk.lib.iterable.utils import get_campaign_id

                if resend:
                    campaign_key = 'triggered.transactional.account.confirm_email_resend'
                else:
                    campaign_key = 'triggered.transactional.account.sign_up_confirm_email'
                itbl_campaign_id = get_campaign_id(campaign_key)

                if itbl_campaign_id:
                    should_send_activation_email = False

                    data = {
                        'activation_uri' : self.get_activation_uri(domain=domain),
                    }

                    itbl = get_iterable_api_client()
                    itbl.send_triggered_email(self.email, itbl_campaign_id, data=data)

            if should_send_activation_email:
                activation_email(self, domain=domain, template=template, subject=subject, sender=sender)
        except:
            request = get_current_request()
            rollbar.report_exc_info(request=request)
Ejemplo n.º 20
0
def check_uuids():
    num_changed = 0

    offset = cache.get(COUNTER_CACHE_NAME) or 0

    players = _get_players(offset)

    if not players:
        rollbar.report_message('All players checked, check_uuids wrapping around', level='info', extra_data={
            'offset': offset
        })
        offset = 0
        players = _get_players(offset)

    for player in players:
        try:
            changed = _handle_player(player)
        except (IntegrityError, OperationalError):
            db.session.rollback()

            rollbar.report_exc_info(level='warning', extra_data={
                'uuid': player.uuid
            })
        else:
            if changed:
                num_changed += 1

    cache.set(COUNTER_CACHE_NAME, offset + PLAYERS_PER_JOB, 86400)

    rollbar.report_message('Finished checking uuid group', level='info', extra_data={
        'offset': offset,
        'num_changed': num_changed
    })
Ejemplo n.º 21
0
    def test_anonymous_tuple_args(self, send_payload):

        # Only run this test on Python versions that support it
        if not _anonymous_tuple_func:
            return

        try:
            _anonymous_tuple_func((1, (2, 3), 4))
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        self.assertEqual(4, len(payload['data']['body']['trace']['frames'][-1]['argspec']))
        self.assertEqual(1, payload['data']['body']['trace']['frames'][-1]['argspec'][0])
        self.assertEqual(2, payload['data']['body']['trace']['frames'][-1]['argspec'][1])
        self.assertEqual(3, payload['data']['body']['trace']['frames'][-1]['argspec'][2])
        self.assertEqual(4, payload['data']['body']['trace']['frames'][-1]['argspec'][3])
        self.assertEqual(10, payload['data']['body']['trace']['frames'][-1]['locals']['ret'])
Ejemplo n.º 22
0
    def test_dont_scrub_star_args(self, send_payload):
        rollbar.SETTINGS['locals']['scrub_varargs'] = False

        def _raise(*args):
            raise Exception()

        try:
            _raise('sensitive', 'text')
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertNotIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('keywordspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('locals', payload['data']['body']['trace']['frames'][-1])

        varargspec = payload['data']['body']['trace']['frames'][-1]['varargspec']

        self.assertEqual(2, len(payload['data']['body']['trace']['frames'][-1]['locals'][varargspec]))
        self.assertEqual(payload['data']['body']['trace']['frames'][-1]['locals'][varargspec][0], 'sensitive')
        self.assertEqual(payload['data']['body']['trace']['frames'][-1]['locals'][varargspec][1], 'text')
Ejemplo n.º 23
0
    def test_args_lambda_with_kwargs_and_args_and_defaults(self, send_payload):

        _raise = lambda arg1, arg2, arg3='default-value', **kwargs: foo(arg1)

        try:
            _raise('a1', 'a2', arg3='arg3-value', arg4=2)
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        keywords = payload['data']['body']['trace']['frames'][-1]['keywordspec']

        # NOTE(cory): again, default values are strange for lambdas and we include them as
        #             positional args.
        self.assertEqual(3, len(payload['data']['body']['trace']['frames'][-1]['argspec']))
        self.assertEqual('arg1', payload['data']['body']['trace']['frames'][-1]['argspec'][0])
        self.assertEqual('arg2', payload['data']['body']['trace']['frames'][-1]['argspec'][1])
        self.assertEqual('arg3', payload['data']['body']['trace']['frames'][-1]['argspec'][2])
        self.assertEqual('a1', payload['data']['body']['trace']['frames'][-1]['locals']['arg1'])
        self.assertEqual('a2', payload['data']['body']['trace']['frames'][-1]['locals']['arg2'])
        self.assertEqual('arg3-value', payload['data']['body']['trace']['frames'][-1]['locals']['arg3'])

        self.assertEqual(1, len(payload['data']['body']['trace']['frames'][-1]['locals'][keywords]))
        self.assertEqual(2, payload['data']['body']['trace']['frames'][-1]['locals'][keywords]['arg4'])
Ejemplo n.º 24
0
    def test_args_generators(self, send_payload):

        def _raise(arg1):
            for i in range(2):
                if i > 0:
                    raise Exception()
                else:
                    yield i

        try:
            l = list(_raise('hello world'))
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        self.assertEqual(1, len(payload['data']['body']['trace']['frames'][-1]['argspec']))
        self.assertEqual('arg1', payload['data']['body']['trace']['frames'][-1]['argspec'][0])
        self.assertEqual('hello world', payload['data']['body']['trace']['frames'][-1]['locals']['arg1'])
Ejemplo n.º 25
0
    def test_args_lambda_with_star_args_and_args(self, send_payload):

        _raise = lambda arg1, *args: foo(arg1)

        try:
            _raise('arg1-value', 1, 2)
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        varargs = payload['data']['body']['trace']['frames'][-1]['varargspec']

        self.assertEqual(1, len(payload['data']['body']['trace']['frames'][-1]['argspec']))
        self.assertEqual('arg1', payload['data']['body']['trace']['frames'][-1]['argspec'][0])
        self.assertEqual('arg1-value', payload['data']['body']['trace']['frames'][-1]['locals']['arg1'])

        self.assertEqual(2, len(payload['data']['body']['trace']['frames'][-1]['locals'][varargs]))
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals'][varargs][0], '\*+')
        self.assertRegex(payload['data']['body']['trace']['frames'][-1]['locals'][varargs][1], '\*+')
Ejemplo n.º 26
0
    def test_args_lambda_with_kwargs_and_args(self, send_payload):

        _raise = lambda arg1, arg2, **kwargs: foo(arg1)

        try:
            _raise('a1', 'a2', arg3='arg3-value', arg4=2)
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertIn('argspec', payload['data']['body']['trace']['frames'][-1])
        self.assertNotIn('varargspec', payload['data']['body']['trace']['frames'][-1])
        self.assertIn('keywordspec', payload['data']['body']['trace']['frames'][-1])

        keywords = payload['data']['body']['trace']['frames'][-1]['keywordspec']

        self.assertEqual(2, len(payload['data']['body']['trace']['frames'][-1]['argspec']))
        self.assertEqual('arg1', payload['data']['body']['trace']['frames'][-1]['argspec'][0])
        self.assertEqual('arg2', payload['data']['body']['trace']['frames'][-1]['argspec'][1])
        self.assertEqual('a1', payload['data']['body']['trace']['frames'][-1]['locals']['arg1'])
        self.assertEqual('a2', payload['data']['body']['trace']['frames'][-1]['locals']['arg2'])

        self.assertEqual(2, len(payload['data']['body']['trace']['frames'][-1]['locals'][keywords]))
        self.assertEqual('arg3-value', payload['data']['body']['trace']['frames'][-1]['locals'][keywords]['arg3'])
        self.assertEqual(2, payload['data']['body']['trace']['frames'][-1]['locals'][keywords]['arg4'])
Ejemplo n.º 27
0
def store_ballot():
    try:
        if request.json is not None:
            division = request.json["division"]
            division_ticket = request.json["division_ticket"]
            senate_ticket = request.json["senate_ticket"]
            order_by_group = request.json["order_by_group"]
        else:
            division = request.form["division"]
            division_ticket = request.form["division_ticket"].split(",")
            senate_ticket = request.form["senate_ticket"].split(",")
            order_by_group = bool(int(request.form["order_by_group"]))

        ballot_id = store_at_random_id(
            {
                "division": division,
                "division_ticket": ",".join(str(x) for x in division_ticket),
                "senate_ticket": ",".join(str(x) for x in senate_ticket),
                "order_by_group": int(order_by_group),
            }
        )

        return jsonify({"ballot_id": ballot_id})
    except:
        rollbar.report_exc_info()
        raise
Ejemplo n.º 28
0
    def test_scrub_self_referencing(self, send_payload):
        def _raise(obj):
            raise Exception()

        try:
            obj = {}
            obj['child'] = {
                'parent': obj
            }

            # NOTE(cory): We copy the dict here so that we don't produce a circular reference
            # from the _rase() args.
            _raise(dict(obj))
        except:
            rollbar.report_exc_info()

        self.assertEqual(send_payload.called, True)

        payload = json.loads(send_payload.call_args[0][0])

        self.assertTrue(
            (isinstance(payload['data']['body']['trace']['frames'][-1]['locals']['obj'], dict) and
             'child' in payload['data']['body']['trace']['frames'][-1]['locals']['obj'])

             or

            (isinstance(payload['data']['body']['trace']['frames'][-1]['locals']['obj'], string_types) and
             payload['data']['body']['trace']['frames'][-1]['locals']['obj'].startswith('<CircularReference'))
        )
Ejemplo n.º 29
0
def report_error():
    data = {'version': plugin.addon.getAddonInfo('version'),
            'platform': platform.system(),
            'machine': platform.machine(),
            'url': plugin.request.url,
            'kodi': kodi_version()}
    rollbar.report_exc_info(extra_data=data)
Ejemplo n.º 30
0
def handle_error(request, exception, exc_info):
    if(
            isinstance(exception, EXCEPTION_BLACKLIST) and
            not isinstance(exception, EXCEPTION_WHITELIST)
    ):
        return
    rollbar.report_exc_info(exc_info, request)
Ejemplo n.º 31
0
def schedule_campaign(client, campaign_id, schedule_time):
    try:
        response = client.campaigns.actions.schedule(campaign_id=campaign_id, data={
            'schedule_time': schedule_time
        })
        current_app.logger.info('Mailchimp campaign {} scheduled for {}'.format(campaign_id, schedule_time))
        return response
    except RequestException as e:
        current_app.logger.error(
            'A Mailchimp API error occurred while scheduling campaign {}, aborting: {} {}'
            .format(campaign_id, e, e.response))
        rollbar.report_exc_info()
        raise e
Ejemplo n.º 32
0
def gridWeight(entity):
    """Unimplemented. Ignore for now.
    """
    try:
        loc = layout.geocode(entity)
        H()
        sprint("%s has an importance weight of %s" %
               (entity.title(), loc.raw['importance']))
        return
    except Exception as e:
        rollbar.report_exc_info()
        logging.error(e)
        gridWeight(entity)
Ejemplo n.º 33
0
    def test_should_add_framework_name_to_payload(self, mock_send_payload, *mocks):
        import rollbar
        from rollbar.contrib.asgi.middleware import ReporterMiddleware

        self.assertIsNone(rollbar.BASE_DATA_HOOK)

        ReporterMiddleware(None)  # invoke integration
        rollbar.report_exc_info()

        self.assertTrue(mock_send_payload.called)
        payload = mock_send_payload.call_args[0][0]

        self.assertIn('asgi', payload['data']['framework'])
Ejemplo n.º 34
0
 def save(self, user, *args, **kwargs):
     user = super(ChangePasswordForm, self).save(*args, **kwargs)
     try:
         password_changed_email(user)
     except:
         from htk.utils.request import get_current_request
         request = get_current_request()
         extra_data = {
             'user' : user,
             'email' : user.profile.confirmed_email or email,
         }
         rollbar.report_exc_info(request=request, extra_data=extra_data)
     return user
Ejemplo n.º 35
0
 def runSlave(self):
     while True:
         try:
             data = self.station_response.recv(1024)
             if data == 'bye':
                 os.kill(self.child_pid, signal.SIGTERM)
             print('Received from the server :',str(data.decode('ascii')))
         except BlockingIOError:
             pass
         except Exception as e:
             rollbar.report_exc_info()
             logger.write("Exception raised at " + datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y") + " Reason: " + str(e) + "\n")
     self.station_response.close()
def delete(finding_id, user_id) -> bool:
    resp = False
    try:
        response = TABLE.delete_item(
            Key={
                'finding_id': finding_id,
                'user_id': user_id
            }
        )
        resp = response['ResponseMetadata']['HTTPStatusCode'] == 200
    except ClientError:
        rollbar.report_exc_info()
    return resp
Ejemplo n.º 37
0
 def handler(loop, context):
     exception = context.get('exception')
     if exception is not None:
         exc_info = (type(exception), exception,
                     exception.__traceback__)
     else:
         exc_info = None
     extra_data = {
         'csbot_event': context.get('csbot_event'),
         'csbot_recent_messages': "\n".join(client.recent_messages),
     }
     rollbar.report_exc_info(exc_info, extra_data=extra_data)
     loop.default_exception_handler(context)
Ejemplo n.º 38
0
def is_allowed_to_emulate_users(user):
    """Determines whether `user` is allowed to emulate other users
    """
    allowed = False
    if user is not None and user.is_authenticated():
        try:
            user_profile = user.profile
            if user_profile.is_company_officer:
                allowed = True
        except:
            request = get_current_request()
            rollbar.report_exc_info(request=request)
    return allowed
Ejemplo n.º 39
0
    def cancel_subscription(self, subscription_id):
        """Cancels a Subscription for this Customer

        https://stripe.com/docs/api#cancel_subscription
        """
        subscription = self.retrieve_subscription(subscription_id)
        if subscription:
            try:
                subscription.delete()
            except stripe.error.InvalidRequestError, e:
                request = get_current_request()
                rollbar.report_exc_info(request=request)
            was_deleted = True
Ejemplo n.º 40
0
 def report_exception(exc_type=None, value=None, tb=None):
     if exc_type is None:
         exc_type, value, tb = sys.exc_info()
     message = ''.join(
         traceback.format_exception(exc_type, value, tb))
     if env.STDOUT_EXCEPTIONS:
         print(message)
     logger.exception(message)
     try:
         rollbar.report_exc_info(exc_info=(exc_type, value, tb),
                                 extra_data={"app": env.APP})
     except Exception as e:
         logger.exception('reporting to rollbar: %s' % e)
Ejemplo n.º 41
0
    async def tick(self):
        action_type_id = await self.read(1)
        action_class = BaseAction.get_class_by_type_id(action_type_id)
        if action_class is None:
            raise ProtocolError(f'Received unknown Action Type ID [{action_type_id.hex()}]', conn=self)

        try:
            await self.handle(await action_class.init(self))
        except self.conf.ignore_errors as err:
            self.close(err)
        except Exception as err:
            rollbar.report_exc_info(extra_data=self.identity_scope_user)  # FIXME: Use plugins/middleware for this
            self.close(err)
def create_team():
    try:
        team = team_business.create_team()
    except TeamError as e:
        abort(e.message)
    except NotFoundError as e:
        return not_found(e.message)
    except UnauthorisedError as e:
        return forbidden(e.message)
    except Exception as e:
        rollbar.report_exc_info()

    return jsonify(team)
Ejemplo n.º 43
0
 def parse_given(self, report: str, opts: list[str]) -> DataStatus:
     """Attempts to parse a given report supplied by the user"""
     try:
         data, code = self._parse_given(report, opts)
         data["meta"] = self.make_meta()
     except Exception as exc:
         print("Unknown Parsing Error", exc)
         rollbar.report_exc_info(extra_data={
             "state": "given",
             "raw": report
         })
         data, code = {"error": ERRORS[1].format(self.report_type)}, 500
     return data, code
Ejemplo n.º 44
0
 def _send_to_rollbar(self, level, text, exception, data):
     if self._rollbar_activated():
         try:
             if exception:
                 rollbar.report_exc_info(sys.exc_info(),
                                         extra_data=data,
                                         level=level)
             else:
                 rollbar.report_message(text, level, extra_data=data)
         except Exception as e:
             plpy.warning(
                 'Error sending message/exception to rollbar: {0}'.format(
                     e))
Ejemplo n.º 45
0
 def report_exception(exc_type=None, value=None, tb=None):
     global project
     if exc_type is None:
         exc_type, value, tb = sys.exc_info()
     stacktrace = ''.join(
         traceback.format_exception(exc_type, value, tb))
     print(stacktrace)
     logger.exception('Exception: %s' % stacktrace)
     try:
         rollbar.report_exc_info(exc_info=(exc_type, value, tb),
                                 extra_data={"app": env.project})
     except Exception as e:
         logger.exception('reporting to rollbar: %s' % e)
def authorise_application(id):
    application = data_api_client.get_application(id)
    if not can_user_view_application(application):
        abort(403, 'Not authorised to access application')
    if is_application_submitted(application):
        return redirect(url_for('.submit_application', id=id))

    application = application['application']
    url = url_for('main.render_application',
                  id=id,
                  step='submit',
                  _external=True)
    user_json = data_api_client.get_user(email_address=application['email'])
    template = 'emails/create_authorise_email_has_account.html'

    if not user_json:
        token_data = {
            'id': id,
            'name': application['representative'],
            'email_address': application['email']
        }
        token = generate_application_invitation_token(token_data)
        url = url_for('main.render_create_application',
                      token=token,
                      _external=True)
        template = 'emails/create_authorise_email_no_account.html'

    email_body = render_template(
        template,
        url=url,
        name=application['representative'],
        business_name=application['name'],
    )

    try:
        send_email(application['email'], email_body,
                   current_app.config['AUTHREP_EMAIL_SUBJECT'],
                   current_app.config['INVITE_EMAIL_FROM'],
                   current_app.config['INVITE_EMAIL_NAME'])
    except EmailError as e:
        rollbar.report_exc_info()
        current_app.logger.error(
            'Authorisation email failed to send. '
            'error {error}',
            extra={'error': six.text_type(e)})
        abort(503, 'Failed to send user invite reset')

    return render_template('suppliers/authorisation_submitted.html',
                           name=application['representative'],
                           email_address=application['email'],
                           subject=current_app.config['AUTHREP_EMAIL_SUBJECT'])
Ejemplo n.º 47
0
async def update_parser(parser: avwx.Report,
                        err_station: "stringable" = None) -> (dict, int):
    """
    Updates the data of a given parser and returns any errors

    Attempts to fetch five times before giving up
    """
    rtype = parser.__class__.__name__.upper()
    state_info = {
        "state": "fetch",
        "station": parser.station,
        "source": parser.service
    }
    # Update the parser's raw data
    try:
        for _ in range(3):
            try:
                if not await parser.async_update(timeout=2, disable_post=True):
                    ierr = 0 if isinstance(err_station, str) else 3
                    return {
                        "error": ERRORS[ierr].format(rtype, err_station)
                    }, 400
                break
            except aio.TimeoutError:
                pass
        else:
            return {
                "error": ERRORS[5].format(parser.service.__class__.__name__)
            }, 502
    except aio.CancelledError:
        print("Cancelled Error")
        return {"error": "Server rebooting. Try again"}, 503
    except ConnectionError as exc:
        print("Connection Error:", exc)
        rollbar.report_exc_info(extra_data=state_info)
        return {"error": str(exc)}, 502
    except avwx.exceptions.SourceError as exc:
        print("Source Error:", exc)
        rollbar.report_exc_info(extra_data=state_info)
        return {"error": str(exc)}, int(str(exc)[-3:])
    except avwx.exceptions.InvalidRequest as exc:
        print("Invalid Request:", exc)
        return {"error": ERRORS[0].format(rtype, err_station)}, 400
    except Exception as exc:
        print("Unknown Fetching Error", exc)
        rollbar.report_exc_info(extra_data=state_info)
        return {"error": ERRORS[4].format(rtype)}, 500
    # Parse the fetched data
    try:
        parser._post_update()
    except avwx.exceptions.BadStation as exc:
        print("Unknown Station:", exc)
        return {"error": ERRORS[2].format(parser.station)}, 400
    except Exception as exc:
        print("Unknown Parsing Error", exc)
        state_info["state"] = "parse"
        state_info["raw"] = parser.raw
        rollbar.report_exc_info(extra_data=state_info)
        return {"error": ERRORS[1].format(rtype), "raw": parser.raw}, 500
    return None, None
Ejemplo n.º 48
0
 async def handle_experience_exit_loop(self):
     while True:
         logger.debug("Checking current experience state for exit...")
         try:
             if (self._current and (await self._current.environment.state())
                     == EnvironmentState.FAILED):
                 logger.error(
                     "Environment failed, attempting to set current experience to empty..."
                 )
                 await self.set_experience(None, throttle=5)
         except Exception as e:
             rollbar.report_exc_info(e)
             logger.exception("Error while handling experience exit loop")
         await asyncio.sleep(1)
Ejemplo n.º 49
0
 def log_audit_event(self, **kwargs):
     try:
         audit = AuditEvent(
             audit_type=kwargs['audit_type'],
             user=kwargs['user'],
             data=kwargs['data'],
             db_object=kwargs['db_object']
         )
         self.save(audit)
     except Exception:
         rollbar.report_exc_info(extra_data={
             "audit_type": kwargs['audit_type'],
             "id": kwargs['db_object'].id
         })
Ejemplo n.º 50
0
def redirect_ballot(ballot_id):
    try:
        r = redis.StrictRedis(host=REDIS_HOST, db=REDIS_DB)

        if not r.exists(ballot_id):
            abort(404)

        division = r.hget(ballot_id, 'division')
        url = 'http://belowtheline.org.au/editor/{}#{}'.format(
            division, ballot_id)
        return redirect(url)
    except:
        rollbar.report_exc_info()
        raise
Ejemplo n.º 51
0
def report_error(error, exc_info, request=None, extra_data=None):
    """Wrapper for error reporting

    This can be used for store exceptions in error reporting solutions as
    rollbar while handling error gracefully and giving user cleaner message.
    """
    if HAS_ROLLBAR and hasattr(settings, 'ROLLBAR'):
        rollbar.report_exc_info(exc_info, request, extra_data=extra_data)

    weblate.logger.error(
        'Handled exception %s: %s',
        error.__class__.__name__,
        str(error)
    )
def _publish_event_batches_to_kinesis(event_batches: List[DialogEventBatch]) -> None:
    kinesis = get_boto3_client("kinesis")
    stage = os.environ.get("STAGE")
    stream_name = f"dialog-event-batches-{stage}"
    records = [
        {
            "PartitionKey": event_batch.phone_number,
            "Data": event_batch.json(),
        }
        for event_batch in event_batches
    ]
    response = kinesis.put_records(StreamName=stream_name, Records=records)
    if response.get("FailedRecordCount"):
        rollbar.report_exc_info(extra_data=response)
Ejemplo n.º 53
0
 def debug_error_handler(e):
     if env == "production":
         rollbar.report_exc_info()
         return json_api(InternalServerError, ErrorSchema), 500
     else:
         return (
             json_api(
                 InternalServerError(
                     detail=str(e), backtrace=traceback.format_exc().split("\n")
                 ),
                 ErrorSchema,
             ),
             500,
         )
Ejemplo n.º 54
0
def short_url(request, code):
    host = request.get_host()
    short_url = resolve_short_url_code(host, code)
    if short_url:
        try:
            short_url.record_request(request)
        except:
            rollbar.report_exc_info(request=request)
        url = short_url.url
        response = redirect(url)
    else:
        default_url = 'http://%s' % htk_setting('HTK_CANONICAL_DOMAIN')
        response = redirect(default_url)
    return response
Ejemplo n.º 55
0
    def save(self, site, request, commit=True):
        domain = request.get_host()
        uri = request.META.get('HTTP_REFERER', '')

        feedback = super(FeedbackForm, self).save(commit=False)
        feedback.site = site
        feedback.uri = uri
        feedback.save()

        try:
            feedback_email(feedback, domain=domain)
        except:
            rollbar.report_exc_info(request=request)
        return feedback
Ejemplo n.º 56
0
def _parse_given(report_type: str, report: str, opts: [str]) -> (dict, int):
    """
    Attempts to parse a given report supplied by the user
    """
    try:
        handler = _HANDLE_MAP[report_type]("KJFK")  # We ignore the station
        handler.update(report)
        resp = asdict(handler.data[0])
        resp["meta"] = {"timestamp": datetime.utcnow()}
        return resp, 200
    except Exception as exc:
        print("Unknown Parsing Error", exc)
        rollbar.report_exc_info(extra_data={"state": "given", "raw": report})
        return {"error": ERRORS[1].format(report_type)}, 500
Ejemplo n.º 57
0
    def send_activation_email(self,
                              domain=None,
                              resend=False,
                              template=None,
                              subject=None,
                              sender=None):
        """Sends an activation email
        """
        domain = domain or htk_setting('HTK_DEFAULT_EMAIL_SENDING_DOMAIN')
        self._reset_activation_key(resend=resend)

        try:
            should_send_activation_email = True

            if htk_setting('HTK_ITERABLE_ENABLED'):
                from htk.lib.iterable.utils import get_iterable_api_client
                from htk.lib.iterable.utils import get_campaign_id

                if resend:
                    campaign_key = 'triggered.transactional.account.confirm_email_resend'
                else:
                    campaign_key = 'triggered.transactional.account.sign_up_confirm_email'
                itbl_campaign_id = get_campaign_id(campaign_key)

                if itbl_campaign_id:
                    should_send_activation_email = False

                    data = {
                        'activation_uri':
                        self.get_activation_uri(domain=domain),
                    }

                    itbl = get_iterable_api_client()
                    itbl.send_triggered_email(self.email,
                                              itbl_campaign_id,
                                              data=data)

            if should_send_activation_email:
                from htk.utils.security import should_use_https
                use_https = should_use_https()

                activation_email(self,
                                 use_https=use_https,
                                 domain=domain,
                                 template=template,
                                 subject=subject,
                                 sender=sender)
        except:
            request = get_current_request()
            rollbar.report_exc_info(request=request)
def update_campaign_content(client, campaign_id, email_body):
    try:
        response = client.campaigns.content.update(
            campaign_id=campaign_id, data={'html': email_body.encode('utf-8')})

        current_app.logger.info(
            'Content updated for Mailchimp campaign {}'.format(campaign_id))
        return response
    except RequestException as e:
        current_app.logger.error(
            'A Mailchimp API error occurred while updating content for campaign {}, aborting: {} {}'
            .format(campaign_id, e, e.response))
        rollbar.report_exc_info()
        raise e
Ejemplo n.º 59
0
def pdf():
    try:
        division_ticket = request.form['division_ticket'].split(',')
        senate_ticket = request.form['senate_ticket'].split(',')
        pdf = generate(request.form['division'], division_ticket,
                       request.form['state'], senate_ticket)
        response = make_response(pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = \
     'attachment; filename=ballot.pdf'
        return response
    except:
        rollbar.report_exc_info()
        raise
Ejemplo n.º 60
0
def login_authenticated_user(request, authenticated_user, backend=None):
    """Logs in an authenticated user and performs related updates
    `authenticated_user` has already been authenticated via one of the login backends
    """
    login(request, authenticated_user, backend=backend)
    authenticated_user.profile.update_locale_info_by_ip_from_request(request)

    if htk_setting('HTK_ITERABLE_ENABLED'):
        try:
            from htk.lib.iterable.utils import get_iterable_api_client
            itbl = get_iterable_api_client()
            itbl.notify_login(authenticated_user)
        except:
            rollbar.report_exc_info()