Metadata-Version: 2.4
Name: starlette-problem
Version: 0.13.3
Summary: Starlette support for RFC9457 problems.
Project-URL: homepage, https://github.com/NRWLDev/starlette-problem/
Project-URL: documentation, https://nrwldev.github.io/starlette-problem/
Author-email: Daniel Edgecombe <daniel@nrwl.co>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: exception,handler,starlette,webdev
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet
Classifier: Topic :: Utilities
Requires-Python: >=3.10
Requires-Dist: rfc9457>=0.4.0
Provides-Extra: dev
Requires-Dist: changelog-gen>=0.13.8; extra == 'dev'
Requires-Dist: pre-commit>=4.5.1; extra == 'dev'
Requires-Dist: pytest-asyncio>=1.3.0; extra == 'dev'
Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
Requires-Dist: pytest-httpx>=0.36.0; extra == 'dev'
Requires-Dist: pytest-random-order>=1.2.0; extra == 'dev'
Requires-Dist: pytest>=9.0.2; extra == 'dev'
Requires-Dist: ruff>=0.14.10; extra == 'dev'
Requires-Dist: starlette; extra == 'dev'
Requires-Dist: uvicorn; extra == 'dev'
Description-Content-Type: text/markdown

# Starlette 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/starlette_problem.svg)](https://pypi.org/project/starlette-problem/)
[![image](https://img.shields.io/pypi/l/starlette_problem.svg)](https://pypi.org/project/starlette-problem/)
[![image](https://img.shields.io/pypi/pyversions/starlette_problem.svg)](https://pypi.org/project/starlette-problem/)
![style](https://github.com/NRWLDev/starlette-problem/actions/workflows/style.yml/badge.svg)
![tests](https://github.com/NRWLDev/starlette-problem/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/NRWLDev/starlette-problem/branch/main/graph/badge.svg)](https://codecov.io/gh/NRWLDev/starlette-problem)

`starlette_problem` is a set of exceptions and handlers for use in starlette
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/starlette-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 starlette_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 starlette.applications
from starlette_problem.handler import add_exception_handler


app = starlette.applications.Starlette()
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,
}
```
