Skip to main content

A package that implements problem details errors in Flask App

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_problem_details-3.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_problem_details-3.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file flask_problem_details-3.0.0.tar.gz.

File metadata

  • Download URL: flask_problem_details-3.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.6

File hashes

Hashes for flask_problem_details-3.0.0.tar.gz
Algorithm Hash digest
SHA256 75b5d4ca2c087e5b2b74f5c76b84bc782772a5837c24e6d276b2e2f18890e207
MD5 87dfc7c0c4010b81994b6e9a15d4b792
BLAKE2b-256 ed0f17af2ccf6fd7796aa334e0fd58b25c80f08c8a3cfe30fb092ec4c609f300

See more details on using hashes here.

File details

Details for the file flask_problem_details-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_problem_details-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0f548039c5ac30e4849b4b642e560339a2c7887f31e7c05a33cbd44a83c5ff8
MD5 922b3dfe74e17f4d4cf9f2fffce526c3
BLAKE2b-256 5a015af0be26665d1176a656e752c64adfdcd5ebbfe84ee547bfa1aeeabb151f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page