Skip to main content

A structured Python exception with diagnostic output and actionable remediation hints.

Project description

๐ŸŽฏsimplibs-exception

PyPI Python Licence

An exception that tries to be a friend.

A structured Python exception framework that doesn't just crashโ€”it explains what went wrong, why it happened, and how to fix it. Beautiful terminal layouts, programmatic analytics exporters, zero filler.

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โš ๏ธ VALIDATION ERROR: parameter age
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Expected:  a positive integer
Got:       -5 (int)
Problem:   value is negative
File info: File: main.py | Line: 42 | Function: validate
File path: /path/to/project/main.py
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐Ÿ”ง How to fix:
     โ€ข Provide a value greater than 0.
     โ€ข Use the int type.
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿงญ The Core Philosophy

Standard Python exceptions tell you where code broke, but they leave you guessing about what was inside your variables or how to recover. You end up stuffing complex debugging telemetry into ad-hoc f-strings.

simplibs-exception changes the paradigm. Instead of chaotic crashes, you get diagnostic reports: highly structured, easily readable context containing your expected vs. obtained values, specific problem statements, and actionable remediation steps.


๐Ÿ“ฆ Installation

pip install simplibs-exception

โš ๏ธ Migration Note (v0.2.x โ†’ v1.0.0+):
Please note that version 1.0.0 and above introduces a breaking change and is not backward compatible with previous releases. The parameter value_label in SimpleExceptionData (and SimpleException) has been renamed to label for greater semantic precision and broader context applicability. If you are upgrading from an older version, please update your codebase accordingly.


๐Ÿš€ Quick Start in 60 Seconds

You can initialize SimpleException as an empty marker and enrich it incrementally. All parameters are optional.

Level 1: The Marker

Just mark a failure pointโ€”the framework automatically captures file, line, and function context.

from simplibs.exception import SimpleException

raise SimpleException()
# โš ๏ธ ERROR: File: main.py | Line: 12 | Function: process

Level 2: Custom Text Message

Use it like a traditional exception but wrapped in cleaner console formatting.

raise SimpleException("Database connection timed out after 30s")

Level 3: The Full Diagnostic

Provide full structured data to turn a simple crash into an elite debugging card.

raise SimpleException(
    label      = "parameter age",
    expected   = "a positive integer",
    value      = age,
    problem    = "value is negative",
    how_to_fix = "Provide a value greater than 0."
)

๐Ÿ› ๏ธ The Architecture: 3 Pillars

To keep the library clean and highly optimized, the logic is separated into three decoupled components:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   SimpleExceptionSettings      โ”‚ โ—„โ”€โ”€ Global configuration, overrides, and safety locks
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      SimpleExceptionData       โ”‚ โ—„โ”€โ”€ Immutable data storage, serializers (.to_dict())
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        SimpleException         โ”‚ โ—„โ”€โ”€ Active runtime manager, dynamic MRO, and execution
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1. SimpleExceptionData (The Model)

Acts as the passive data schema. It owns the raw parameters, lazy caching properties, and public analytics state exporters (to_dict(), to_json()).

โžก๏ธ README_DATA.md

2. SimpleException (The Runtime Engine)

The active execution manager. It overrides dataclass-level __init__ routines, manages dynamic Method Resolution Order (MRO) injection, triggers subclass compile-time audits, and orchestrates terminal rendering.

โžก๏ธ README_EXCEPTION.md

3. SimpleExceptionSettings (The Control Center)

A validated global configuration interface governed by a strict metaclass. It lets you customize default behaviors, set value truncation boundaries, and control stack trace filtering securely.

โžก๏ธ README_SETTINGS.md


๐ŸŽจ Output Modes

Your exception can speak different visual formats depending on your deployment environment:

  • PRETTY (Default): Beautifully framed visual card layout optimized for console output during development.
  • SIMPLE: Plain-text structured layout without Unicode borders. Perfect for standard files and log processors.
  • ONELINE: Everything compressed into a single line. Perfect for standard application log streaming.
  • LOG: Highly optimized, machine-readable key=value format designed for log forwarders (Datadog, Splunk, ELK).

โžก๏ธ README_MODES.md

You can easily build your own layout formats (e.g., HTML, Slack message blocks) by subclassing the base template manager ModeBase.

โžก๏ธ README_CUSTOM_MODES.md


๐Ÿงฐ Built-in Developer Tools

The framework provides helper functions to minimize repetitive error-handling boilerplate:

bool_or_exception

A bi-modal validation shortcut. It either gracefully returns False or raises a fully-structured exception based on a flag, eliminating verbose conditional block code.

from simplibs.exception.tools import bool_or_exception

def validate_age(age: int, return_bool: bool = False) -> bool:
    if age < 0:
        return bool_or_exception(return_bool, value=age, label="age", expected="positive integer")
    return True

โžก๏ธ README_BOOL_OR_EXCEPTION.md

raise_with_location_offset

A single-line utility that takes any exception, dynamically shifts its stack trace tracking depth backwards, and raises it.

raise_with_location_offset(SimpleException("Failed"), offset=1)

โžก๏ธ README_RAISE_WITN_LOCATION_OFFSET.md

@raise_location_offset (Decorator)

An Aspect-Oriented decorator that intercepts bubbling exceptions and re-targets their trace location to the caller's call-site. Excellent for writing transparent gatekeeper and validator helpers.

@raise_location_offset(offset=1)
def assert_positive(n: int):
    if n <= 0:
        raise SimpleException(value=n, expected="strictly positive")

โžก๏ธ README_RAISE_LOCATION_OFFSET.md


๐Ÿงช Advanced Testing Ecosystem

Writing unit tests for custom exceptions, validation functions, and error parameters often leads to repetitive boilerplate. To solve this, simplibs-exception includes a complete, self-contained testing framework under its testing namespace.

With our testing tools, you can run bulk matrix-driven evaluations, validate class MRO integrity, and audit error payloads with minimal code:

from simplibs.exception.testing import assert_exception_class

# Automatically audits inheritance, defaults, constructors, and serialization protocols
assert_exception_class(MyCustomError)

โžก๏ธ README_TESTING.md


โ˜ฏ๏ธ About simplibs

All libraries in the simplibs (Simple Libraries) ecosystem share a common engineering philosophy:

  • Dyslexia-friendly: We actively minimize cognitive load. Code is atomized into small, self-contained units, files are named directly after the logical task they perform, and explanations describe why something is designed, not just what it is.
  • Programmer's Zen: Nothing should be missing, and nothing should be superfluous. We value clean execution paths and robust, understandable code architectures over rushed, messy feature sets.
  • Defensive Style: We actively anticipate edge cases and failure modes so that only safe operational paths remain. Our code is built to degrade gracefully rather than crash unexpectedly.
  • Minimalism: Find the most direct path to the goal in as few operational steps as possible without taking shortcuts on safety, readability, or completeness.
  • Code as Craft: Code should be pleasant to look at, readable at a glance, and evoke structural harmony. We treat software engineering as a precision trade.

๐Ÿค Contributing & Community

This is an open-source project built with love and care. We strongly believe in community collaboration and welcome any feedback, bug reports, or feature ideas!

  • Want to contribute? Feel free to open an Issue or submit a Pull Request.
  • Want to get in touch? If you'd like to discuss the project further, collaborate, or just say hello, feel free to open a GitHub Issue or start a Discussion.

๐Ÿ“ License

This library is released under the MIT License. Build great things!


โ–ฒ Back to Top

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

simplibs_exception-1.0.1.tar.gz (105.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simplibs_exception-1.0.1-py3-none-any.whl (160.0 kB view details)

Uploaded Python 3

File details

Details for the file simplibs_exception-1.0.1.tar.gz.

File metadata

  • Download URL: simplibs_exception-1.0.1.tar.gz
  • Upload date:
  • Size: 105.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for simplibs_exception-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ced5870c26c48b24910f619acbaab8cba5dfaf824a1409a0ee3d108a30b770e7
MD5 09d46f0c167323b70306d61ae23533a3
BLAKE2b-256 8888514ac351c47dd6a2765294d4e6a0f7f773521d73be616c83f678e03cac19

See more details on using hashes here.

File details

Details for the file simplibs_exception-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simplibs_exception-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14a76cfacc8cff9ad4cc0ae7706f1b8de7aa60c3522a356d94769d567102f4bd
MD5 d2dc0f364987fdfc88727c91965936cb
BLAKE2b-256 c541cc1b0066c23e9b28a2647aca3d248b63700b92798a8e3ef267aacdd70cb2

See more details on using hashes here.

Supported by

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