# errr
Elegantly create detailed exceptions in Python.
## Detailed exceptions
```python
>>> import errr
>>> class MyException(errr.DetailedException, list_detailts=True, details=["cause", "type"]):
... pass
...
>>> example = MyException("The backend server crashed", "backend", "crash")
>>> raise example
__main__.MyException: The backend server crashed
Details:
˪cause: backend
˪type: crash
>>> example.details
{'cause': 'backend', 'type': 'crash'}
>>> example.cause
'backend'
```
## Semantic exceptions
You can also rapidly create large semantic trees of exceptions using the `make_tree`
function, listing exceptions as keyword arguments using the `errr.exception` factory
method. The `make_tree` method executes these recursive factories to produce your
exceptions. Nesting these factory methods will make the resultant exceptions inherit from
eachother. All of the produced exceptions are then flat injected into the given module
dictionary (typically) this should be `globals()` but you can inject into other modules
using `sys.modules["name"].__dict__`.
```python
from errr import make_tree, exception as _e
make_tree(
# Pass the module dictionary as first argument
globals(),
# List your nested exceptions
RootException=_e(
ChildException=_e(),
Child2Exception=_e()
),
SecondRootException=_e(
# List details as positional arguments
"detail1", "detail2",
# And continue with child exceptions as keyword arguments
AnotherChildException=_e()
)
)
print(RootException)
# <class '__main__.RootException'>
print(ChildException)
# <class '__main__.ChildException'>
print(ChildException.__bases__)
# (<class '__main__.RootException'>,)
```
## Exception wrapping
You can catch and reraise exceptions as a new type of exception with the `wrap` function:
```python
import errr
class LibraryError(errr.DetailedException, details=["library"]):
pass
for name, library in libraries.items():
try:
library.load()
except Exception as e:
errr.wrap(LibraryError, e, name, prepend="When trying to load %library% it reported:\n")
# Traceback
# ...
# __main__.LibraryError: When trying to load myLibrary it reported:
# Module 'missing' not found.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Helveg/errr",
"name": "errr",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Robin De Schepper",
"author_email": "robingilbert.deschepper@unipv.it",
"download_url": "https://files.pythonhosted.org/packages/31/60/d9685a931dd93f89d4372b789a7bb10ba95b4d83d177b874ba0914552562/errr-1.2.0.tar.gz",
"platform": null,
"description": "# errr\r\nElegantly create detailed exceptions in Python.\r\n\r\n## Detailed exceptions\r\n\r\n```python\r\n>>> import errr\r\n>>> class MyException(errr.DetailedException, list_detailts=True, details=[\"cause\", \"type\"]):\r\n... pass\r\n...\r\n>>> example = MyException(\"The backend server crashed\", \"backend\", \"crash\")\r\n>>> raise example\r\n__main__.MyException: The backend server crashed\r\n\r\nDetails:\r\n \u00cb\u00aacause: backend\r\n \u00cb\u00aatype: crash\r\n>>> example.details\r\n{'cause': 'backend', 'type': 'crash'}\r\n>>> example.cause\r\n'backend'\r\n```\r\n\r\n## Semantic exceptions\r\n\r\nYou can also rapidly create large semantic trees of exceptions using the `make_tree`\r\nfunction, listing exceptions as keyword arguments using the `errr.exception` factory\r\nmethod. The `make_tree` method executes these recursive factories to produce your\r\nexceptions. Nesting these factory methods will make the resultant exceptions inherit from\r\neachother. All of the produced exceptions are then flat injected into the given module\r\ndictionary (typically) this should be `globals()` but you can inject into other modules\r\nusing `sys.modules[\"name\"].__dict__`.\r\n\r\n```python\r\nfrom errr import make_tree, exception as _e\r\n\r\nmake_tree(\r\n # Pass the module dictionary as first argument\r\n globals(),\r\n # List your nested exceptions\r\n RootException=_e(\r\n ChildException=_e(),\r\n Child2Exception=_e()\r\n ),\r\n SecondRootException=_e(\r\n # List details as positional arguments\r\n \"detail1\", \"detail2\",\r\n # And continue with child exceptions as keyword arguments\r\n AnotherChildException=_e()\r\n )\r\n)\r\n\r\nprint(RootException)\r\n# <class '__main__.RootException'>\r\nprint(ChildException)\r\n# <class '__main__.ChildException'>\r\nprint(ChildException.__bases__)\r\n# (<class '__main__.RootException'>,)\r\n```\r\n\r\n## Exception wrapping\r\n\r\nYou can catch and reraise exceptions as a new type of exception with the `wrap` function:\r\n\r\n```python\r\nimport errr\r\n\r\nclass LibraryError(errr.DetailedException, details=[\"library\"]):\r\n pass\r\n\r\nfor name, library in libraries.items():\r\n try:\r\n library.load()\r\n except Exception as e:\r\n errr.wrap(LibraryError, e, name, prepend=\"When trying to load %library% it reported:\\n\")\r\n# Traceback\r\n# ...\r\n# __main__.LibraryError: When trying to load myLibrary it reported:\r\n# Module 'missing' not found.\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Elegant detailed Python exceptions.",
"version": "1.2.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "ce0dbf9abc081cf9595a1845f85b3fa6",
"sha256": "adc785eef4d6cb2072f19bdcb051f4b818bf6ce1036a606b1917284b45ca8807"
},
"downloads": -1,
"filename": "errr-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ce0dbf9abc081cf9595a1845f85b3fa6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5877,
"upload_time": "2022-11-23T10:30:15",
"upload_time_iso_8601": "2022-11-23T10:30:15.502516Z",
"url": "https://files.pythonhosted.org/packages/c8/44/10aac23018bd13b52b0cff7d6d44079c117a808322448ffd4e0d05546835/errr-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "a23d23ab2662db6365e86d9dc8c18591",
"sha256": "2824b83edbd1f61d354c60e36400b2e31ab83e2094acb837b86686892b866e76"
},
"downloads": -1,
"filename": "errr-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "a23d23ab2662db6365e86d9dc8c18591",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5432,
"upload_time": "2022-11-23T10:30:16",
"upload_time_iso_8601": "2022-11-23T10:30:16.696078Z",
"url": "https://files.pythonhosted.org/packages/31/60/d9685a931dd93f89d4372b789a7bb10ba95b4d83d177b874ba0914552562/errr-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-11-23 10:30:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Helveg",
"github_project": "errr",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "errr"
}