fastapi-problem


Namefastapi-problem JSON
Version 0.10.1 PyPI version JSON
download
home_pageNone
SummaryFastAPI support for RFC9457 problems.
upload_time2024-11-14 15:36:55
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseApache-2.0
keywords exception handler starlette webdev
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastAPI Problems
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
[![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![image](https://img.shields.io/pypi/v/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)
[![image](https://img.shields.io/pypi/l/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)
[![image](https://img.shields.io/pypi/pyversions/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)
![style](https://github.com/NRWLDev/fastapi-problem/actions/workflows/style.yml/badge.svg)
![tests](https://github.com/NRWLDev/fastapi-problem/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/NRWLDev/fastapi-problem/branch/main/graph/badge.svg)](https://codecov.io/gh/NRWLDev/fastapi-problem)

`fastapi_problem` is a set of exceptions and handlers for use in fastapi
applications to support easy error management and responses.

Each exception easily marshals to JSON based on the
[RFC9457](https://www.rfc-editor.org/rfc/rfc9457.html) spec for use in api
errors.

Check the [docs](https://nrwldev.github.io/fastapi-problem) for more details.

## Custom Errors

Subclassing the convenience classes provide a simple way to consistently raise
the same error with detail/extras changing based on the raised context.

```python
from fastapi_problem.error import NotFoundProblem


class UserNotFoundError(NotFoundProblem):
    title = "User not found."

raise UserNotFoundError(detail="detail")
```

```json
{
    "type": "user-not-found",
    "title": "User not found",
    "detail": "detail",
    "status": 404,
}
```

## Usage

```python
import fastapi
from fastapi_problem.handler import add_exception_handler


app = fastapi.FastAPI()
add_exception_handler(app)

@app.get("/user")
async def get_user():
    raise UserNotFoundError("No user found.")
```

```bash
$ curl localhost:8000/user
{

    "type": "user-not-found",
    "title": "User not found",
    "detail": "No user found.",
    "status": 404,
}
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-problem",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "exception, handler, starlette, webdev",
    "author": null,
    "author_email": "Daniel Edgecombe <daniel@nrwl.co>",
    "download_url": "https://files.pythonhosted.org/packages/1a/7b/81cd53a09fdb81f1cb12080e9211127c0729071bf90759deaa2acc203849/fastapi_problem-0.10.1.tar.gz",
    "platform": null,
    "description": "# FastAPI Problems\n[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)\n[![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![image](https://img.shields.io/pypi/v/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)\n[![image](https://img.shields.io/pypi/l/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)\n[![image](https://img.shields.io/pypi/pyversions/fastapi_problem.svg)](https://pypi.org/project/fastapi-problem/)\n![style](https://github.com/NRWLDev/fastapi-problem/actions/workflows/style.yml/badge.svg)\n![tests](https://github.com/NRWLDev/fastapi-problem/actions/workflows/tests.yml/badge.svg)\n[![codecov](https://codecov.io/gh/NRWLDev/fastapi-problem/branch/main/graph/badge.svg)](https://codecov.io/gh/NRWLDev/fastapi-problem)\n\n`fastapi_problem` is a set of exceptions and handlers for use in fastapi\napplications to support easy error management and responses.\n\nEach exception easily marshals to JSON based on the\n[RFC9457](https://www.rfc-editor.org/rfc/rfc9457.html) spec for use in api\nerrors.\n\nCheck the [docs](https://nrwldev.github.io/fastapi-problem) for more details.\n\n## Custom Errors\n\nSubclassing the convenience classes provide a simple way to consistently raise\nthe same error with detail/extras changing based on the raised context.\n\n```python\nfrom fastapi_problem.error import NotFoundProblem\n\n\nclass UserNotFoundError(NotFoundProblem):\n    title = \"User not found.\"\n\nraise UserNotFoundError(detail=\"detail\")\n```\n\n```json\n{\n    \"type\": \"user-not-found\",\n    \"title\": \"User not found\",\n    \"detail\": \"detail\",\n    \"status\": 404,\n}\n```\n\n## Usage\n\n```python\nimport fastapi\nfrom fastapi_problem.handler import add_exception_handler\n\n\napp = fastapi.FastAPI()\nadd_exception_handler(app)\n\n@app.get(\"/user\")\nasync def get_user():\n    raise UserNotFoundError(\"No user found.\")\n```\n\n```bash\n$ curl localhost:8000/user\n{\n\n    \"type\": \"user-not-found\",\n    \"title\": \"User not found\",\n    \"detail\": \"No user found.\",\n    \"status\": 404,\n}\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "FastAPI support for RFC9457 problems.",
    "version": "0.10.1",
    "project_urls": null,
    "split_keywords": [
        "exception",
        " handler",
        " starlette",
        " webdev"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2e7371e1013410410372b1c6ee4b655f695719b2aa28f65b74662af9fff5978e",
                "md5": "12b47acbc9d4f84a41a6359577a65177",
                "sha256": "b2c74fdfd30c7b9ff126e2a1630d441d62e7b11272fa2f0a0998af2bdb47f1f1"
            },
            "downloads": -1,
            "filename": "fastapi_problem-0.10.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "12b47acbc9d4f84a41a6359577a65177",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8501,
            "upload_time": "2024-11-14T15:36:53",
            "upload_time_iso_8601": "2024-11-14T15:36:53.647026Z",
            "url": "https://files.pythonhosted.org/packages/2e/73/71e1013410410372b1c6ee4b655f695719b2aa28f65b74662af9fff5978e/fastapi_problem-0.10.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1a7b81cd53a09fdb81f1cb12080e9211127c0729071bf90759deaa2acc203849",
                "md5": "396441c270b14827a7903b366cebe961",
                "sha256": "d1904d0562ec9ea35451a1f907876a9deca9b5c52a81b65d0b3eb73cdf6ff126"
            },
            "downloads": -1,
            "filename": "fastapi_problem-0.10.1.tar.gz",
            "has_sig": false,
            "md5_digest": "396441c270b14827a7903b366cebe961",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 66637,
            "upload_time": "2024-11-14T15:36:55",
            "upload_time_iso_8601": "2024-11-14T15:36:55.446696Z",
            "url": "https://files.pythonhosted.org/packages/1a/7b/81cd53a09fdb81f1cb12080e9211127c0729071bf90759deaa2acc203849/fastapi_problem-0.10.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-14 15:36:55",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "fastapi-problem"
}
        
Elapsed time: 5.04578s