Python Custom Exceptions
A collection of structured, ready-to-use exception classes for Python projects.
Every exception renders as JSON with its type, a human-readable message, and optional diagnostic info,
so errors are easy to read in logs and easy to parse by tools.
Features
- ✅ Specific Exception Classes: Ready-made exceptions for common error scenarios.
- ✅ Structured Output:
str(exception)is a JSON document with type, message and diagnostic info. - ✅ Diagnostic Info: Attach typed context (
TypedDict,dictor dataclass) to any exception. - ✅ Single Base Class: Everything inherits from
BaseCustomException, so oneexceptcatches them all. - ✅ Easy Extension: Subclass
BaseCustomExceptionto add your own exceptions.
Installation
uv add python-custom-exceptions
or with pip:
pip install python-custom-exceptions
Requires Python 3.12+.
Available Exceptions
| Exception | Arguments | Message |
|---|---|---|
BaseCustomException |
message_with_marking_dynamic_variables |
<message> |
ComparisonException |
expected_value, actual_value, subject=None |
Expected <subject> to be [<expected>], but got [<actual>] |
ConnectionException |
host, message=None |
Connection failed: Unable to establish a connection to the remote host ["<host>"]. ... |
IsExistException |
subject |
[<subject>] is exists |
IsNotExistException |
subject |
[<subject>] is not exists |
IsExistAfterOperationException |
subject, operation |
<subject> exists after trying to <operation> |
IsNotExistAfterOperationException |
subject, operation |
<subject> does not exist after trying to <operation> |
OperationException |
operation |
Operation <operation> failed |
NotOperationalException |
subject |
<subject> is not Operational |
UnexpectedlySucceededOperationException |
operation |
Operation <operation> unexpectedly succeeded |
TimedExecutionException |
operation, timeout |
Operation <operation> did not succeed within [<timeout>] seconds |
UnsupportedExceptions |
unsupported_subject |
<unsupported_subject> is unsupported |
UnsupportedOSExceptions |
os_name |
Unsupported OS: ["<os_name>"] |
WrongUsageException |
message |
Wrong usage: <message> |
All exceptions also accept an optional diagnostic_info argument.
Usage Examples
Raise an exception with diagnostic info
from pathlib import Path
from python_custom_exceptions import DiagnosticInfo, IsNotExistException
class PathDiagnosticInfo(DiagnosticInfo):
path: str
path = Path("/tmp/missing")
if not path.exists():
raise IsNotExistException(
subject="Directory",
diagnostic_info=PathDiagnosticInfo(path=str(path)),
)
Output:
{
"exception_type": "IsNotExistException",
"message": "[Directory] is not exists",
"diagnostic_info": {
"path": "/tmp/missing"
}
}
Catch any custom exception
from python_custom_exceptions import BaseCustomException, ComparisonException
try:
raise ComparisonException(expected_value=200, actual_value=500, subject="status code")
except BaseCustomException as exc:
print(exc.exception_type) # ComparisonException
print(exc.message) # Expected status code to be [200], but got [500]
print(exc.diagnostic_info) # {}
Define your own exception
from python_custom_exceptions import BaseCustomException, DiagnosticInfo
class QuotaExceededException(BaseCustomException):
def __init__(self, resource: str, limit: int, diagnostic_info: dict | DiagnosticInfo | None = None) -> None:
super().__init__(
message_with_marking_dynamic_variables=f"Quota for [{resource}] exceeded the limit of [{limit}]",
diagnostic_info=diagnostic_info,
)
Development
git clone https://github.com/aviz92/python-custom-exceptions.git
cd python-custom-exceptions
uv sync # create .venv and install runtime + dev dependencies
uv run pre-commit install # enable git hooks
uv run pytest # run the tests
uv build # build sdist + wheel into dist/
🤝 Contributing
If you have a helpful exception, pattern, or improvement to suggest:
- Fork the repo
- Create a new branch
- Submit a pull request
I welcome additions that promote clean, productive, and maintainable development.
🙏 Thanks
Thanks for exploring this repository!
Happy coding!
Metadata
Release files for python-custom-exceptions 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_custom_exceptions-1.0.0.tar.gz | 9.5 kB | Without provenance | Details for python_custom_exceptions-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_custom_exceptions-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for python_custom_exceptions-1.0.0-py3-none-any.whl |
Total release size: 18.7 kB
Release files / python_custom_exceptions-1.0.0.tar.gz
| Download URL | python_custom_exceptions-1.0.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
871ee1052bfcaecc25d8d65fd1c04295babb0964eb4c0e709a161f1757ac801a
|
|
BLAKE2b-256 checksum How to use checksums |
8801dd02f3bf828387f41f565167922afe1bf89b9b6c9d0989f6b089bb970568
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / python_custom_exceptions-1.0.0-py3-none-any.whl
| Download URL | python_custom_exceptions-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
01159c20d122c0b4b7b134013e79a2c04fb7e6287888975cb82888112bae15ff
|
|
BLAKE2b-256 checksum How to use checksums |
2c3972a5a20bfd5926a6e0f192104a23b8544e448a3d13a357ad2aebce47a8cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for python-custom-exceptions 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.