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.1.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.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for flask_problem_details-3.0.1.tar.gz
Algorithm Hash digest
SHA256 0e3181420044aeafaa3b0b078bb46b89f4516014fad882bbb607b397d28c369a
MD5 ed0c7c6ba8cd373f570b74a39c58a188
BLAKE2b-256 9b1bcd650cf4ab81ce5c50f06f80e37862217a67d89a15bf5a2269962b8671f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_problem_details-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c22c40493f151de3a037b014449bdbc9ae6acdbbad118b2c0ac2e5999d270168
MD5 7515718db009d4ae99f7b528577f72d2
BLAKE2b-256 e01cebb7d4da415400bf14a406f9b7142fa0549e68bf6bc472855ef2f03ec92c

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