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):

This makes it easier to catch and handle exactly the problem I want to handle:

except QueryTookTooLong:
except ServerRejectedQueryBecauseItWasBusy:
except Exception:
class SuperCoolPackageError(Exception, ABC):
    """All exceptions in this library are instances of a SuperCoolPackageError"""

class QueryTookTooLong(TimeoutError, SuperCoolPackageError):

Provide structured data in the exception

class QueryTookTooLong(TimeoutError):
    server_address: str
    query_type: str

The SqlAlchemy library provides links to documentation about an error in the exception string. This makes reading up about the error much easier

Thoughts? Comments? Send me a tweet!