Skip to main content

No project description provided

Project description

xcept

xcept is a package for the convenience of creating exceptions.


Installation

pip install xcept

Usage

To create a new exception class, you need to inherit from BaseException_ from the xcept package and specify a dataclass decorator:

from xcept import BaseException_
from dataclasses import dataclass


@dataclass
class ExampleException(BaseException_):
    foo: str
    bar: int

To create an exception object, you need to pass a message template and keyword arguments:

try:
    raise ExampleException("error {foo} {bar}!", foo="test", bar=12345)
except ExampleException as exception:
    print(exception)  # error test 12345!
    print(repr(exception))  # ExampleException(foo='test', bar=12345)
    raise


# Traceback (most recent call last):
#   raise ExampleException("error {foo} {bar}!", foo="test", bar=12345)
# ExampleException: error test 12345!

By default, all arguments are required to be inserted into the template. If there are no arguments in the template, this will lead to an error:

ExampleException("error {bar}!", foo="test", bar=12345)


# Traceback (most recent call last):
#   ExampleException("error {bar}!", foo="test", bar=12345)
# xcept.errors.UnusedKeywordArgumentError: keyword argument 'foo' is not used!

If you don't want to have a detailed message, you can define ALLOW_UNUSED_ARGS = True:

from xcept import BaseException_
from dataclasses import dataclass


@dataclass
class ExampleException(BaseException_):    
    ALLOW_UNUSED_ARGS = True
    foo: str
    bar: int


raise ExampleException("error {bar}!", foo="test", bar=12345)


# Traceback (most recent call last):
#   raise ExampleException("error {bar}!", foo="test", bar=12345)
# ExampleException: error 12345!

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

xcept-1.0.0.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

xcept-1.0.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

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