Skip to main content
Help us improve Python packaging – donate today!

Customized Exception class

Project Description

This is an error class that I keep remaking in projects

https://travis-ci.org/delfick/delfick_error.png?branch=master

Usage

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()
DelfickError('',)
>>> str(DelfickError())
''
>>> str(DelfickError("blah"))
'"blah"'
>>> str(DelfickError("blah", a=1, b=2))
'"blah"\ta=1\tb=2'

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

>>> class AnError(DelfickError):
...  desc = "An error specific to something"
...
>>> AnError()
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"
errors:
        Bad value
        Bad index
        blah
>>>

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))
'a=1\tb=20_formatted_b'

Changelog

1.2
Tests work in python26, python27 and python34
1.1

Now with tests!

And DelfickErrorTestMixin for your testing pleasure

1.0
Initial release

Installation

Use pip!:

pip install delfick_error

Or if you’re developing it:

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

Tests

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

./test.sh

Or run tox:

tox

Release history Release notifications

History Node

1.7.8

History Node

1.7.7

History Node

1.7.6.1

History Node

1.7.6

History Node

1.7.5

History Node

1.7.4

History Node

1.7.3

History Node

1.7.2

History Node

1.7.1

History Node

1.7

History Node

1.6.1

History Node

1.6

History Node

1.5

History Node

1.4

History Node

1.3

This version
History Node

1.2

History Node

1.1

History Node

1.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
delfick_error-1.2.tar.gz (3.8 kB) Copy SHA256 hash SHA256 Source None Aug 18, 2014

Supported by

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