Skip to main content

Customized Exception class

Project description

This is an error class that I keep remaking in projects


The point of this exception class is to be able to create an error class that automatically combines keyword arguments given to the exception instance:

>>> from delfick_error import DelfickError
>>> DelfickError()
>>> str(DelfickError())
>>> str(DelfickError("blah"))
>>> str(DelfickError("blah", a=1, b=2))

You can also subclass DelfickError and override the description of the error:

>>> class AnError(DelfickError):
...  desc = "An error specific to something"
>>> AnError()
>>> str(AnError("things"))
'"An error specific to something. things"'
>>> str(AnError("things", a=1, b=2))
'"An error specific to something. things"\ta=1\tb=2'

You can also use it to display multiple errors:

>>> ex1 = ValueError("Bad value")
>>> ex2 = IndexError("Bad index")
>>> ex3 = Exception("blah")
>>> str(AnError("found errors", _errors=[ex1, ex2, ex3]))
'"An error specific to something. found errors"\nerrors:\n\tBad value\n\tBad index\n\tblah'
>>> print str(AnError("found errors", _errors=[ex1, ex2, ex3]))
"An error specific to something. found errors"
        Bad value
        Bad index

Here, the _errors argument is interpreted using special logic to put each item in it’s list on a new line.

DelfickError also has a mechanism to allow you to control how the error is formatted when converting the error to a string.

>>> class SomeObject(object):
...   def __init__(self, val):
...     self.val = val
...   def delfick_error_format(self, key):
...     return "{0}_formatted_{1}".format(self.val, key)
>>> obj = SomeObject(20)
>>> str(DelfickError(a=1, b=obj))


Use pip!:

pip install delfick_error

Or if you’re developing it:

pip install -e .
pip install -e ".[tests]"


To run the tests in this project, just use the helpful script:


Or run tox:


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for delfick-error, version 1.0
Filename, size File type Python version Upload date Hashes
Filename, size delfick_error-1.0.tar.gz (2.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page