RFC 9457 Problem Details models for Jentic APIs
Project description
jentic-problem-details (Python)
RFC 9457 Problem Details models for Jentic APIs.
Installation
pip install jentic-problem-details
For FastAPI integration:
pip install jentic-problem-details[fastapi]
Quick Start
Basic Usage
from jentic.problem_details import ProblemDetail, ErrorItem
# Create a problem detail response
problem = ProblemDetail(
status=400,
title="Bad Request",
detail="The request body is missing required field 'name'.",
instance="/v2/capability-sets",
errors=[
ErrorItem(
detail="Field 'name' is required.",
pointer="#/name"
)
]
)
print(problem.model_dump_json(exclude_none=True))
FastAPI Integration
from fastapi import FastAPI, Request
from jentic.problem_details import (
BadRequest,
NotFound,
ProblemDetailException,
problem_detail_exception_handler,
)
app = FastAPI()
# Register the exception handler
app.add_exception_handler(ProblemDetailException, problem_detail_exception_handler)
@app.get("/users/{user_id}")
async def get_user(user_id: str):
# Input validation
if not user_id:
raise BadRequest(
detail="User ID is required",
instance="/users",
code="JENTIC-4001"
)
# Resource not found
user = await db.get_user(user_id)
if not user:
raise NotFound(
detail=f"User '{user_id}' not found",
instance=f"/users/{user_id}"
)
return user
@app.post("/users")
async def create_user(request: Request):
data = await request.json()
# Validation with multiple errors
errors = []
if not data.get("name"):
errors.append({"detail": "Field 'name' is required", "pointer": "#/name"})
if not data.get("email"):
errors.append({"detail": "Field 'email' is required", "pointer": "#/email"})
if errors:
raise BadRequest(
detail="The request body is missing required fields",
instance="/users",
errors=errors
)
user = await db.create_user(data)
return user
Response Format
All exceptions produce responses with Content-Type: application/problem+json:
{
"type": "about:blank",
"status": 400,
"title": "Bad Request",
"detail": "The request body is missing required fields",
"instance": "/users",
"errors": [
{
"detail": "Field 'name' is required",
"pointer": "#/name"
}
]
}
Available Exceptions
| Exception | Status | Description |
|---|---|---|
BadRequest |
400 | Client error (malformed syntax, invalid parameters) |
Unauthorized |
401 | Authentication required or failed |
Forbidden |
403 | Server refuses to authorize the request |
NotFound |
404 | Resource does not exist |
Conflict |
409 | Request conflicts with current state |
ValidationError |
422 | Request is well-formed but semantically invalid |
TooManyRequests |
429 | Rate limit exceeded |
ServerError |
500 | Unexpected server error |
ServiceUnavailable |
503 | Server temporarily unable to handle request |
All exceptions extend ProblemDetailException which accepts:
detail(required): Human-readable explanationtype: URI identifying the problem type (default: "about:blank")title: Short summary (default: standard HTTP phrase)instance: Request path or URI referencecode: Provider-specific error codeerrors: List ofErrorItemfor granular validation errorsheaders: Additional HTTP headers
Error Items
For validation errors with multiple fields, use the errors array:
from jentic.problem_details import ValidationError, ErrorItem
raise ValidationError(
detail="Multiple validation errors occurred",
instance="/api/resources",
errors=[
ErrorItem(detail="Must be a positive integer", parameter="limit"),
ErrorItem(detail="Must be 'asc' or 'desc'", parameter="order"),
ErrorItem(detail="Must be a valid email", pointer="#/email"),
ErrorItem(detail="Authorization header is malformed", header="Authorization"),
]
)
Development
# Install dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type checking
mypy src/
Standards
License
jentic-problem-details is licensed under Apache 2.0 license. jentic-problem-details comes with an explicit NOTICE file containing additional legal notices and information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jentic_problem_details-1.0.1.tar.gz.
File metadata
- Download URL: jentic_problem_details-1.0.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
709b53bf9c73a3541e3192a64efa110612735a0b5e4d32ba1ebe5d085a6edf18
|
|
| MD5 |
b80693900e7d3da1883f0b5b65edef37
|
|
| BLAKE2b-256 |
1a088967e9a483411dabb19a25950a7e2a8cf065fea3935d928ea33469aa1c57
|
Provenance
The following attestation bundles were made for jentic_problem_details-1.0.1.tar.gz:
Publisher:
release.yaml on jentic/api-problem-details
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_problem_details-1.0.1.tar.gz -
Subject digest:
709b53bf9c73a3541e3192a64efa110612735a0b5e4d32ba1ebe5d085a6edf18 - Sigstore transparency entry: 1293636710
- Sigstore integration time:
-
Permalink:
jentic/api-problem-details@07f6ef901b465f99c2745e683e91fc5950107c75 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@07f6ef901b465f99c2745e683e91fc5950107c75 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file jentic_problem_details-1.0.1-py3-none-any.whl.
File metadata
- Download URL: jentic_problem_details-1.0.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
481b3ad268dacd471e9e12fe64624527cf3ef9f1002ba265676adef386d45ab0
|
|
| MD5 |
e0d06b5d76a0764d4fb403224460cf94
|
|
| BLAKE2b-256 |
fccfbb3cf3f70f14986eda218aeb36c981016dd4de7cb5865cfc171087e62df7
|
Provenance
The following attestation bundles were made for jentic_problem_details-1.0.1-py3-none-any.whl:
Publisher:
release.yaml on jentic/api-problem-details
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_problem_details-1.0.1-py3-none-any.whl -
Subject digest:
481b3ad268dacd471e9e12fe64624527cf3ef9f1002ba265676adef386d45ab0 - Sigstore transparency entry: 1293636716
- Sigstore integration time:
-
Permalink:
jentic/api-problem-details@07f6ef901b465f99c2745e683e91fc5950107c75 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@07f6ef901b465f99c2745e683e91fc5950107c75 -
Trigger Event:
workflow_dispatch
-
Statement type: