I’m using the python logging integration.
The way I like to define exceptions in python3 is to define a frozen dataclass for them, and have a couple fields of relevant data to be passed in. For example:
from dataclasses import dataclass @dataclass(frozen=True) class MyError(Exception): error_code: int magic_word: str
So an exception like that would be raised like:
if some_error_condition: raise MyError(404, 'error not found')
and handled like:
try: something_that_raises_as_above() except MyError as exc: logger.exception(exc)
So then I’d get an event in Sentry with the exception class as the main heading (I like this), but the ‘message’ section when I click into it just has the argument tuple:
What I want:
- the Exception class to define a property that composes a human-readable error message based on the fields. e.g. for the above exception, ‘Error code 404: event_not_found’
- the fields to appear as tags in Sentry
What I’ve tried:
I tried to find some documentation on how an exception object is converted into a Sentry message, but I was not successful.
I also defined a
message property on my error class to compose the human-readable message, and altered the capture line to
And that worked for the human-readable message bit. I’d prefer not to need to reach into the exception object in the logger.exception call though - I’d much prefer to define that in the exception class.