web-error


Nameweb-error JSON
Version 0.5.6 PyPI version JSON
download
home_pagehttps://github.com/EdgyEdgemond/web-error/
SummaryWeb based error utils
upload_time2023-08-10 15:01:25
maintainer
docs_urlNone
authorDaniel Edgecombe
requires_python>=3.8,<4.0
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Web Errors v0.5.6
[![image](https://img.shields.io/pypi/v/web_error.svg)](https://pypi.org/project/web_error/)
[![image](https://img.shields.io/pypi/l/web_error.svg)](https://pypi.org/project/web_error/)
[![image](https://img.shields.io/pypi/pyversions/web_error.svg)](https://pypi.org/project/web_error/)
![style](https://github.com/EdgyEdgemond/web-error/workflows/style/badge.svg)
![tests](https://github.com/EdgyEdgemond/web-error/workflows/tests/badge.svg)
[![codecov](https://codecov.io/gh/EdgyEdgemond/web-error/branch/master/graph/badge.svg)](https://codecov.io/gh/EdgyEdgemond/web-error)

`web_error` is a set of exceptions and handlers for use in web apis to support easy error management and responses

Each exception easily marshals to JSON for use in api errors. Handlers for different web frameworks are provided.


## Errors

The base `web_error.error.HttpException` accepts a `message`, `debug_message`, `code` and `status` (default 500)

And will render a response with status as the status code:

```json
{
    "code": "code",
    "message": "message",
    "debug_message": "debug_message",
}
```

Some convenience Exceptions are provided, to create custom error subclass these
and define `message` and `code` attributes.

* `web_error.error.ServerException` provides status 500 errors
* `web_error.error.BadRequestException` provides status 400 errors
* `web_error.error.UnauthorisedException` provides status 401 errors
* `web_error.error.NotFoundException` provides status 404 errors

### Custom Errors

Subclassing the convenience classes provide a simple way to consistently raise the same error
and message.

Code is an optional attribute to provide a unique value to parse in a frontend/client instead of
matching against messages.

```python
from web_error.error import NotFoundException


class UserNotFoundError(NotFoundException):
    message = "User not found."
    code = "E001"
```


## FastAPI

Include the `exception_handler` in your app.

```python
    exception_handler = web_error.handler.fastapi.exception_handler

    return FastAPI(
        exception_handlers={
            Exception: exception_handler,
            RequestValidationError: exception_handler,
            HTTPException: exception_handler,
        },
    )
```
## Pyramid

Include the pyramid exception handlers in your config.

```python
def main(global_config, **config_blob):
    config = Configurator(settings=config_blob)

    ...

    config.scan("web_error.handler.pyramid")

    return config.make_wsgi_app()
```

This will handle all unexpected errors, and any app specific errors.

```python
@view_config(route_name="test", renderer="json")
def test(request):
    raise UserNotFoundError("debug message")
```


## Flask

Register the error handler with your application

```python
app.register_error_handler(Exception, web_error.handler.flask.exception_handler)
```

## Aiohttp

Decorate your views with the error handler.

```python
@web_error.handler.aiohttp.view_error_handler
async def user(self):
    raise UserNotFoundError("debug message")
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/EdgyEdgemond/web-error/",
    "name": "web-error",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Daniel Edgecombe",
    "author_email": "edgy.edgemond@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/57/71/209a7be11c52277bf27ffc26a15dd774e7b60178d00631e2a19f494b88bc/web_error-0.5.6.tar.gz",
    "platform": null,
    "description": "# Web Errors v0.5.6\n[![image](https://img.shields.io/pypi/v/web_error.svg)](https://pypi.org/project/web_error/)\n[![image](https://img.shields.io/pypi/l/web_error.svg)](https://pypi.org/project/web_error/)\n[![image](https://img.shields.io/pypi/pyversions/web_error.svg)](https://pypi.org/project/web_error/)\n![style](https://github.com/EdgyEdgemond/web-error/workflows/style/badge.svg)\n![tests](https://github.com/EdgyEdgemond/web-error/workflows/tests/badge.svg)\n[![codecov](https://codecov.io/gh/EdgyEdgemond/web-error/branch/master/graph/badge.svg)](https://codecov.io/gh/EdgyEdgemond/web-error)\n\n`web_error` is a set of exceptions and handlers for use in web apis to support easy error management and responses\n\nEach exception easily marshals to JSON for use in api errors. Handlers for different web frameworks are provided.\n\n\n## Errors\n\nThe base `web_error.error.HttpException` accepts a `message`, `debug_message`, `code` and `status` (default 500)\n\nAnd will render a response with status as the status code:\n\n```json\n{\n    \"code\": \"code\",\n    \"message\": \"message\",\n    \"debug_message\": \"debug_message\",\n}\n```\n\nSome convenience Exceptions are provided, to create custom error subclass these\nand define `message` and `code` attributes.\n\n* `web_error.error.ServerException` provides status 500 errors\n* `web_error.error.BadRequestException` provides status 400 errors\n* `web_error.error.UnauthorisedException` provides status 401 errors\n* `web_error.error.NotFoundException` provides status 404 errors\n\n### Custom Errors\n\nSubclassing the convenience classes provide a simple way to consistently raise the same error\nand message.\n\nCode is an optional attribute to provide a unique value to parse in a frontend/client instead of\nmatching against messages.\n\n```python\nfrom web_error.error import NotFoundException\n\n\nclass UserNotFoundError(NotFoundException):\n    message = \"User not found.\"\n    code = \"E001\"\n```\n\n\n## FastAPI\n\nInclude the `exception_handler` in your app.\n\n```python\n    exception_handler = web_error.handler.fastapi.exception_handler\n\n    return FastAPI(\n        exception_handlers={\n            Exception: exception_handler,\n            RequestValidationError: exception_handler,\n            HTTPException: exception_handler,\n        },\n    )\n```\n## Pyramid\n\nInclude the pyramid exception handlers in your config.\n\n```python\ndef main(global_config, **config_blob):\n    config = Configurator(settings=config_blob)\n\n    ...\n\n    config.scan(\"web_error.handler.pyramid\")\n\n    return config.make_wsgi_app()\n```\n\nThis will handle all unexpected errors, and any app specific errors.\n\n```python\n@view_config(route_name=\"test\", renderer=\"json\")\ndef test(request):\n    raise UserNotFoundError(\"debug message\")\n```\n\n\n## Flask\n\nRegister the error handler with your application\n\n```python\napp.register_error_handler(Exception, web_error.handler.flask.exception_handler)\n```\n\n## Aiohttp\n\nDecorate your views with the error handler.\n\n```python\n@web_error.handler.aiohttp.view_error_handler\nasync def user(self):\n    raise UserNotFoundError(\"debug message\")\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Web based error utils",
    "version": "0.5.6",
    "project_urls": {
        "Homepage": "https://github.com/EdgyEdgemond/web-error/",
        "Repository": "https://github.com/EdgyEdgemond/web-error/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b0c24c2050e0ca568a828757ed1f5799ae964c0adda107adff80a6ee5c72f6c",
                "md5": "c9c30d36dae7e0d6129d1430046171db",
                "sha256": "a7d19d87f9bda25e5301be53e4caf8988f91cd7a79469b39a983ba5c94448419"
            },
            "downloads": -1,
            "filename": "web_error-0.5.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c9c30d36dae7e0d6129d1430046171db",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 12173,
            "upload_time": "2023-08-10T15:01:24",
            "upload_time_iso_8601": "2023-08-10T15:01:24.205402Z",
            "url": "https://files.pythonhosted.org/packages/8b/0c/24c2050e0ca568a828757ed1f5799ae964c0adda107adff80a6ee5c72f6c/web_error-0.5.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5771209a7be11c52277bf27ffc26a15dd774e7b60178d00631e2a19f494b88bc",
                "md5": "e4292024e59645243466716d6a136da4",
                "sha256": "bd040daedb585215c6c8fa5d6237bc2df679930f3e2e8224fd6e18c95aef697e"
            },
            "downloads": -1,
            "filename": "web_error-0.5.6.tar.gz",
            "has_sig": false,
            "md5_digest": "e4292024e59645243466716d6a136da4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 10695,
            "upload_time": "2023-08-10T15:01:25",
            "upload_time_iso_8601": "2023-08-10T15:01:25.375900Z",
            "url": "https://files.pythonhosted.org/packages/57/71/209a7be11c52277bf27ffc26a15dd774e7b60178d00631e2a19f494b88bc/web_error-0.5.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-10 15:01:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "EdgyEdgemond",
    "github_project": "web-error",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "web-error"
}
        
Elapsed time: 2.18918s