Write more useful exceptions in python
This is an unpublished post. It's likely (more) full of spelling mistakes and errors. It may also end abruptly without a conclusion.
Why I’m writing this
I’ve recently been tidying up error reporting at work. There’s a few noisy exceptions coming from some third party libraries and older code. I’d like to be able to filter them out programmatically so that they don’t end up in sentry.
However, I’ve been running into a problems when the exceptions are raised like this:
raise TimeoutError("When trying to do the thing we timed out for reason X")
I’m having to do a combination of rewriting code and some fragile string matching on the error messages so that I can filter out what’s a really important error and what is more like warning.
What would I prefer
How could this be improved:
Sub typed errors
For example there are lots of different things that can timeout. Rather than throwing the generic exception libraries could subclass it:
class QueryTookTooLong(TimeoutError):
pass
This makes it easier to catch and handle exactly the problem I want to handle:
try:
do_something()
except QueryTookTooLong:
record_slow_query():
except ServerRejectedQueryBecauseItWasBusy:
back_off_and_try_a_different_server()
except Exception:
panic_an_error_we_didnt_know_about_happened()
Link errors to the library
class SuperCoolPackageError(Exception, ABC):
"""All exceptions in this library are instances of a SuperCoolPackageError"""
pass
class QueryTookTooLong(TimeoutError, SuperCoolPackageError):
pass
Provide structured data in the exception
class QueryTookTooLong(TimeoutError):
server_address: str
query_type: str
Provide a link to documentation about this error (bonus points for this one)
The SqlAlchemy library provides links to documentation about an error in the exception string. This makes reading up about the error much easier