How do you raise a python exception and include additional data for Sentry? How do you raise a python exception and include additional data for Sentry? django django

How do you raise a python exception and include additional data for Sentry?


I log exceptions using the logging library so after debugging the code a bit, I noticed the extra parameter:

import logginglogger = logging.getLogger('my_app_name')def do_something():    try:        #do some stuff here that might break    except Exception, e:        logger.error(e, exc_info=1, extra={'extra-data': 'blah', })

Passing exc_info=1 is the same as calling logger.exception. However, exception() does not accept kwargs, which are required for using the extra parameter.

These values will show up in the 'Additional Data' section of the Sentry Error dashboard.


wes' answer didn't help me because I want to actually raise an exception, not only log it.

Here's what I did (client is the Raven Sentry client):

client.extra_context({'foo': 'bar'})raise RuntimeError('Whoops, something went wrong!')


You might try one of these two approaches:

>>> # Raise the exception with the data you want.>>> raise Exception('extra information')Traceback (most recent call last):  File "<pyshell#64>", line 1, in <module>    raise Exception('extra information')Exception: extra information>>> # Catch an exception and add extra arguments.>>> try:    raise Exception()except Exception as error:    error.args += ('extra information',)    raiseTraceback (most recent call last):  File "<pyshell#68>", line 2, in <module>    raise Exception()Exception: extra information>>> 

You can add as many additional data fields as you want by adding more arguments.