Skip to main content

That's not flying, it's falling with style: Exceptions with extras

Project description

Latest Version Build Status

py-buzz

That’s not flying, it’s falling with style: Exceptions with extras

This package is a light-weight extension of python’s Exception class with some added niceties

Requirements

  • Python 3

Installing

Install using pip:

$ pip install py-buzz

Using

Just import:

from buzz import Buzz
raise Buzz("something went wrong!")

Buzz also makes an excellent base-class for your project specific exception classes!

Features

Automatic message formatting

Buzz exception messages can be automatically formatted with format arguments and keyword arguments. This saves a few characters and generally makes the exception code a little easier to read:

# Vanilla python
raise Exception("a {} message".format('formatted'))
raise Exception("a {fmt} message".format(fmt='formatted'))

# With py-buzz
raise Buzz("a {} message", 'formatted')
raise Buzz("a {fmt} message", fmt='formatted')

Obviously the benefits aren’t that easy to see with a small message with few args, but getting rid of an additional level of nesting can be nice with more complex messages

Raise exception on condition failure

Buzz provides a function that checks a condition and raises an exception if it fails. This is nice, because you often find your self writing a lot of if <whatever>: raise Exception(<message>) throughout your code base. Buzz makes it just a little easier to write and read that kind of code:

# Vanilla python
if not some_condition():
    raise Exception("some_condition failed")

# With py-buzz
Buzz.require_condition(some_condition(), "some_condition failed")

This is again justa bit of syntactic sugar but can make code a bit more palletable, especially if you have to check a lot of invariants in a function

Exception handling context manager

Buzz also provides a context manager that catches any exceptions that might be caught in executing a bit of code. The caught exceptions are re-packaged as Buzz errors and re-raised with a message that captures the initial exception’s message:

# Vanilla python
try:
   this_could_fail()
   this_could_also_fail()
   this_will_definitely_fail()
except Exception as err:
    raise Exception("Something didn't work -- Error: {}".format(str(err)))

# With py-buzz
with Buzz.handle_errors("Something didn't work"):
   this_could_fail()
   this_could_also_fail()
   this_will_definitely_fail()

This actually can save a bit of code and makes things a bit cleaner. It is also a pattern that tends to get repeated over and over again. If more complex error handling logic is needed, this context manager shouldn’t be used. It is intended to just handle simple re-packaging of exceptions in a block of code

Project details


Download files

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

Source Distribution

py-buzz-0.1.11.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file py-buzz-0.1.11.tar.gz.

File metadata

  • Download URL: py-buzz-0.1.11.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py-buzz-0.1.11.tar.gz
Algorithm Hash digest
SHA256 23202bff3e75fa4b4a3f608fd3239697da8cd11338bdbec00a0e159d3e677196
MD5 52ac6f75e45fe67f933c075a1a831965
BLAKE2b-256 3b7538005fc33d81756b768bdcc9c479b71a9d4be992a591c56ae2c4b82c0965

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page