Skip to content

Commit

Permalink
simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
ezarowny committed Jun 28, 2016
1 parent 4cff2c2 commit 0b2e63f
Showing 1 changed file with 19 additions and 38 deletions.
57 changes: 19 additions & 38 deletions rollbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def report_message(message, level='error', request=None, extra_data=None, payloa
log.exception("Exception while reporting message to Rollbar. %r", e)


def send_payload(payload, access_token, uuid, host):
def send_payload(payload, access_token):
"""
Sends a payload object, (the result of calling _build_payload()).
Uses the configured handler from SETTINGS['handler']
Expand All @@ -386,27 +386,27 @@ def send_payload(payload, access_token, uuid, host):
"""
handler = SETTINGS.get('handler')
if handler == 'blocking':
_send_payload(payload, access_token, uuid, host)
_send_payload(payload, access_token)
elif handler == 'agent':
agent_log.error(payload)
elif handler == 'tornado':
if TornadoAsyncHTTPClient is None:
log.error('Unable to find tornado')
return
_send_payload_tornado(payload, access_token, uuid, host)
_send_payload_tornado(payload, access_token)
elif handler == 'gae':
if AppEngineFetch is None:
log.error('Unable to find AppEngine URLFetch module')
return
_send_payload_appengine(payload, access_token, uuid, host)
_send_payload_appengine(payload, access_token)
elif handler == 'twisted':
if treq is None:
log.error('Unable to find Treq')
return
_send_payload_twisted(payload, access_token, uuid, host)
_send_payload_twisted(payload, access_token)
else:
# default to 'thread'
thread = threading.Thread(target=_send_payload, args=(payload, access_token, uuid, host))
thread = threading.Thread(target=_send_payload, args=(payload, access_token))
thread.start()


Expand Down Expand Up @@ -437,13 +437,6 @@ def search_items(title, return_fields=None, access_token=None, endpoint=None, **
**search_fields)


class PayloadTooLargeException(Exception):
"""
This exception will be raised if the payload is too large.
"""
pass


class ApiException(Exception):
"""
This exception will be raised if there was a problem decoding the
Expand Down Expand Up @@ -610,11 +603,9 @@ def _report_exc_info(exc_info, request, extra_data, payload_data, level=None):
data = dict_merge(data, payload_data)

payload = _build_payload(data)
uuid = data['uuid']
host = data['server']['host']
send_payload(payload, data.get('access_token'), uuid, host)
send_payload(payload, data.get('access_token'))

return uuid
return data['uuid']


def _report_message(message, level, request, extra_data, payload_data):
Expand Down Expand Up @@ -645,11 +636,9 @@ def _report_message(message, level, request, extra_data, payload_data):
data = dict_merge(data, payload_data)

payload = _build_payload(data)
uuid = data['uuid']
host = data['server']['host']
send_payload(payload, data.get('access_token'), uuid, host)
send_payload(payload, data.get('access_token'))

return uuid
return data['uuid']


def _check_config():
Expand Down Expand Up @@ -1100,22 +1089,16 @@ def _build_payload(data):
return json.dumps(payload)


def _send_payload(payload, access_token, uuid, host):
def _send_payload(payload, access_token):
try:
_post_api('item/', payload, access_token=access_token)
except PayloadTooLargeException as e:
log.error("Rollbar: request entity too large for UUID %r\n. Payload:\n%r", uuid, payload)
_send_failsafe('payload too large', uuid, host)
except Exception as e:
log.exception('Exception while posting item %r', e)


def _send_payload_appengine(payload, access_token, uuid, host):
def _send_payload_appengine(payload, access_token):
try:
_post_api_appengine('item/', payload, access_token=access_token)
except PayloadTooLargeException as e:
log.error("Rollbar: request entity too large for UUID %r\n. Payload:\n%r", uuid, payload)
_send_failsafe('payload too large', uuid, host)
except Exception as e:
log.exception('Exception while posting item %r', e)

Expand Down Expand Up @@ -1162,12 +1145,9 @@ def _get_api(path, access_token=None, endpoint=None, **params):
return _parse_response(path, access_token, params, resp, endpoint=endpoint)


def _send_payload_tornado(payload, access_token, uuid, host):
def _send_payload_tornado(payload, access_token):
try:
_post_api_tornado('item/', payload, access_token=access_token)
except PayloadTooLargeException as e:
log.error("Rollbar: request entity too large for UUID %r\n. Payload:\n%r", uuid, payload)
_send_failsafe('payload too large', uuid, host)
except Exception as e:
log.exception('Exception while posting item %r', e)

Expand All @@ -1194,12 +1174,9 @@ def _post_api_tornado(path, payload, access_token=None):
_parse_response(path, SETTINGS['access_token'], payload, r)


def _send_payload_twisted(payload, access_token, uuid, host):
def _send_payload_twisted(payload, access_token):
try:
_post_api_twisted('item/', payload, access_token=access_token)
except PayloadTooLargeException as e:
log.error("Rollbar: request entity too large for UUID %r\n. Payload:\n%r", uuid, payload)
_send_failsafe('payload too large', uuid, host)
except Exception as e:
log.exception('Exception while posting item %r', e)

Expand Down Expand Up @@ -1268,7 +1245,11 @@ def _parse_response(path, access_token, params, resp, endpoint=None):
log.warning("Rollbar: over rate limit, data was dropped. Payload was: %r", params)
return
elif resp.status_code == 413:
raise PayloadTooLargeException()
payload = json.loads(params)
uuid = payload['data']['uuid']
host = payload['data']['server']['host']
log.error("Rollbar: request entity too large for UUID %r\n. Payload:\n%r", uuid, payload)
_send_failsafe('payload too large', uuid, host)
elif resp.status_code != 200:
log.warning("Got unexpected status code from Rollbar api: %s\nResponse:\n%s",
resp.status_code, data)
Expand Down

0 comments on commit 0b2e63f

Please sign in to comment.