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 spec for use in api errors.
Check the docs for more details.
Subclassing the convenience classes provide a simple way to consistently raise the same error with detail/extras changing based on the raised context.
from starlette_problem.error import NotFoundProblem
class UserNotFoundError(NotFoundProblem):
title = "User not found."
raise UserNotFoundError(detail="detail")
{
"type": "user-not-found",
"title": "User not found",
"detail": "detail",
"status": 404,
}
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.")
$ curl localhost:8000/user
{
"type": "user-not-found",
"title": "User not found",
"detail": "No user found.",
"status": 404,
}