Skip to main content

Flask Problem Details

Overview

This module enhances Flask (or Flask OpenAPI) applications introducing structured error handling based on the RFC 7807 Problem Details for HTTP APIs. It supports automatic validation error handling and provides detailed error responses, optionally including stack traces.


Features

  • Problem Details Specification: Conforms to the Problem Details for HTTP APIs standard.
  • Automatic Error Handling: Registers handlers for common exceptions like validation errors and server-side issues.
  • Configurable Stack Traces: Optionally include stack traces in error responses for easier debugging.
  • Flask support: The package support Flask and/or Flask OpenAPI 3 applications.

Installation

pip install flask-problem-details

Usage

1. Configure the Application

from flask_openapi3 import OpenAPI, Info
from werkzeug.exceptions import NotImplemented
from flask_problem_details import (
    configure_app, from_exception, ProblemDetails, ProblemDetailsError
)

# OpenAPI information
info: Info = Info(title="Flask OpenAPI 3 Example", version="1.0.0")
openapi_callback = lambda args : OpenAPI(__name__, info=info, **args)

app : OpenAPI = configure_app(app = openapi_callback, with_traceback=True)

@app.get("/authors")
def get_authors():
    raise NotImplemented()

@app.get("/books")
def get_books():
    description: str = "The method is not implemented"
    extras : dict = {"one": "extra value"}
    raise from_exception(NotImplementedError(description), extras = extras)

@app.get("/cats")
def get_cats():
   problem = ProblemDetails(status=412, title = "No shelter")
   raise ProblemDetailsError(problem)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=3000, debug=True)

2. Error Response Example

When an error occurs, the module returns a JSON response similar to:

GET /authors

{
  "status": 501,
  "title": "NotImplemented",
  "detail": "The server does not support the action requested by the browser.",
  "traceback": "Traceback (most recent call last):..."
}

GET /books

{
  "status": 500,
  "title": "InternalServerError",
  "detail": "The method is not implemented",
  "traceback": "Traceback (most recent call last):...",
  "one": "extra value"
}

GET /cats

{
  "status": 412,
  "title": "No shelter",
  "type": "uri:localhost:noshelter",
  "traceback": "Traceback (most recent call last):..."
}

Core Components

Classes

  1. ProblemDetails: A Pydantic model representing the structure of an error response.
  2. ProblemDetailsError: Exception class for handling problems.

Functions

  • configure_app(app, with_traceback=False): Sets up the application with error handling.
  • activate_traceback() / deactivate_traceback(): Enable or disable traceback inclusion.
  • from_exception(exception, extras): create a ProblemDetailsErrors from an exception.

Extending the Module

To add custom error handling, register additional error handlers using Flask's register_error_handler method:

@app.errorhandler(CustomException)
def handle_custom_exception(e):
    problem = ProblemDetails(status=412, title="Error", detail=str(e))
    return problem.to_http_response()

License

This module is provided under the MIT License.


Contributions

Contributions are welcome! Submit a pull request or open an issue on GitHub.


References

Metadata

Release files for flask-problem-details 3.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-problem-details 3.0.1
File Size Uploaded Provenance Actions
flask_problem_details-3.0.1.tar.gz 5.3 kB Without provenance Details for flask_problem_details-3.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-problem-details 3.0.1
File Interpreter ABI Platform Actions
flask_problem_details-3.0.1-py3-none-any.whl Python 3 none any Details for flask_problem_details-3.0.1-py3-none-any.whl

Total release size: 11.1 kB

Release files / flask_problem_details-3.0.1.tar.gz

Download URL flask_problem_details-3.0.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0e3181420044aeafaa3b0b078bb46b89f4516014fad882bbb607b397d28c369a
BLAKE2b-256 checksum
How to use checksums
9b1bcd650cf4ab81ce5c50f06f80e37862217a67d89a15bf5a2269962b8671f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Release files / flask_problem_details-3.0.1-py3-none-any.whl

Download URL flask_problem_details-3.0.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c22c40493f151de3a037b014449bdbc9ae6acdbbad118b2c0ac2e5999d270168
BLAKE2b-256 checksum
How to use checksums
e01cebb7d4da415400bf14a406f9b7142fa0549e68bf6bc472855ef2f03ec92c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Security for flask-problem-details 3.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page