Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make report_exc_info to accept level arg like report_message #22

Merged
merged 2 commits into from May 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions rollbar/__init__.py
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ def init(access_token, environment='production', **kw):
agent_log = _create_agent_log()


def report_exc_info(exc_info=None, request=None, extra_data=None, payload_data=None, **kw):
def report_exc_info(exc_info=None, request=None, extra_data=None, payload_data=None, level=None, **kw):
"""
Reports an exception to Rollbar, using exc_info (from calling sys.exc_info())
@@ -195,7 +195,7 @@ def report_exc_info(exc_info=None, request=None, extra_data=None, payload_data=N
exc_info = sys.exc_info()

try:
return _report_exc_info(exc_info, request, extra_data, payload_data)
return _report_exc_info(exc_info, request, extra_data, payload_data, level=level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying the _report_exc_info() signature, I think it would be better to update payload_data in here like so:

if level:
    payload_data = payload_data or {}
    payload_data['level'] = level

try:
    return _report_exc_info(exc_info, request, extra_data, payload_data)
except:
    # ...

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I purposely did this to follow report_message's style (where if payload_data['level'] is provided it will override provided level arg). So the order is exception level -> level arg -> payload_data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point!

except Exception as e:
log.exception("Exception while reporting exc_info to Rollbar. %r", e)

@@ -361,7 +361,7 @@ def _create_agent_log():
return retval


def _report_exc_info(exc_info, request, extra_data, payload_data):
def _report_exc_info(exc_info, request, extra_data, payload_data, level=None):
"""
Called by report_exc_info() wrapper
"""
@@ -379,6 +379,10 @@ def _report_exc_info(exc_info, request, extra_data, payload_data):
if filtered_level:
data['level'] = filtered_level

# explicitly override the level with provided level
if level:
data['level'] = level

# exception info
# most recent call last
raw_frames = traceback.extract_tb(trace)
31 changes: 31 additions & 0 deletions rollbar/test/test_rollbar.py
Original file line number Diff line number Diff line change
@@ -226,3 +226,34 @@ def test_uuid(self, send_payload):
payload = json.loads(send_payload.call_args[0][0])

self.assertEqual(payload['data']['uuid'], uuid)

@mock.patch('rollbar.send_payload')
def test_report_exc_info_level(self, send_payload):

try:
raise Exception('level_error')
except:
rollbar.report_exc_info()

self.assertEqual(send_payload.called, True)
payload = json.loads(send_payload.call_args[0][0])
self.assertEqual(payload['data']['level'], 'error')

try:
raise Exception('level_info')
except:
rollbar.report_exc_info(level='info')

self.assertEqual(send_payload.called, True)
payload = json.loads(send_payload.call_args[0][0])
self.assertEqual(payload['data']['level'], 'info')

# payload takes precendence over 'level'
try:
raise Exception('payload_warn')
except:
rollbar.report_exc_info(level='info', payload_data={'level': 'warn'})

self.assertEqual(send_payload.called, True)
payload = json.loads(send_payload.call_args[0][0])
self.assertEqual(payload['data']['level'], 'warn')