Throw and catch named exceptions without creating a class first
Project description
EasyExceptions
Throw and catch named exceptions without creating a class first. The goal of this project is to make working with named exceptions simpler. It's often a huge overhead of work to create named exceptions in a separate file before being able to throw them to nicely be caught.
Installation
pip install easy-exceptions
Usage
from easy_exceptions import EasyException
try:
raise EasyException('NamedException')('TEST MESSAGE')
except EasyException('NamedException') as e:
print("Caught this specific exception without having to define it!")
EasyExceptions even supports using a custom parent class instead of the base Exception. This is because APIs often use a custom exception class for rendering error messages. All you have to do in this case is pass that parent class to EasyException and instantiate it the way you always would. For example:
from easy_exceptions import EasyException
from api.exceptions import BaseException # Your custom base exception
try:
# you can pass whatever special args here that you usually do to BaseException
raise EasyException('NamedException', parent=BaseException)(...)
except EasyException('NamedException', parent=BaseException):
print("Caught the specific exception!")
# This exception can still be caught by referencing the BaseException with regular syntax
except BaseException:
print("This would be called if the above exception handler wasn't there")
This can be further simplified by binding the BaseException during application startup using the bind_parent
from easy_exceptions import EasyException, bind_parent
from api.exceptions import BaseException
bind_parent(BaseException)
try:
raise EasyException('NamedException')
except BaseException: #Could alse use EasyException('NamedException') to target the named exception only
print("Exceptions are always of type BaseException now")
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
File details
Details for the file easy_exceptions-1.0.2.tar.gz
.
File metadata
- Download URL: easy_exceptions-1.0.2.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bcb54d9acee8a9cc5afd96acf3a7e858108d55038efddb7279671f4f579bb6a |
|
MD5 | 6c77abc60ac0f777ef83cc0270f2bd14 |
|
BLAKE2b-256 | b9a0a111c7792d8bd6d5c97d1d55738d6ff364b351b8ed992cb1f70cb2f9e2f7 |