izulu: The Exceptional Library

0.75.0 · active · verified Sun Apr 12

izulu is a Python library designed to bring Object-Oriented Programming (OOP) principles into exception and error management. It eliminates the need for manual error message formatting by allowing developers to define exception templates within class definitions. This approach centralizes static error data and uses keyword arguments for variable data, generating final error messages dynamically. The library is actively maintained, with version 0.75.0 being the latest stable release.

Warnings

Install

Imports

Quickstart

Define custom exception classes by inheriting from `izulu.root.Error`. Use the `__template__` class attribute to specify the error message format, incorporating placeholders for dynamic data. Instantiate exceptions with keyword arguments that match the template placeholders and any type-hinted attributes for automatic population. Handle these custom exceptions with standard `try-except` blocks.

from izulu.root import Error

class DataError(Error):
    __template__ = "Invalid data: {reason}"

class MissingField(DataError):
    __template__ = "Missing required field: '{field_name}'"
    field_name: str

class OutOfRange(DataError):
    __template__ = "Value for '{field_name}' is out of range. Expected: {min_val}-{max_val}, Got: {actual_val}"
    field_name: str
    min_val: int
    max_val: int
    actual_val: int

def process_data(data: dict):
    if not data:
        raise DataError(reason="empty input")
    
    if 'name' not in data:
        raise MissingField(field_name='name')
    
    age = data.get('age')
    if not isinstance(age, int):
        raise DataError(reason="age must be an integer")
    
    min_age, max_age = 18, 99
    if not (min_age <= age <= max_age):
        raise OutOfRange(field_name='age', min_val=min_age, max_val=max_age, actual_val=age)
    
    print(f"Data processed successfully for {data['name']} (Age: {age})")

# Example Usage:
try:
    process_data({"name": "Alice", "age": 25})
except DataError as e:
    print(f"Error processing data: {e}")

try:
    process_data(None)
except DataError as e:
    print(f"Error processing data: {e}")

try:
    process_data({"name": "Bob"})
except MissingField as e:
    print(f"Error processing data: {e}. Field: {e.field_name}")

try:
    process_data({"name": "Charlie", "age": 150})
except OutOfRange as e:
    print(f"Error processing data: {e}. Details: field={e.field_name}, actual={e.actual_val}")

view raw JSON →