Skip to main content

Business validation diagnostics (errors) collection library. With FastAPI support.

Project description

GCM Diagnostics

Library for collecting errors from validation logic and presenting them to user.

Mainly intended to be used with FastAPI endpoints for doing business validation which Pydantic is not intended for. Errors generated by this library are compatible with Pydantic validation response, so it appears the same to the consumer of API.

Installation

pip install gcm_diagnostics

Usage

from gcm_diagnostics import DiagnosticCollector
from gcm_diagnostics.errors import EntityNotFound, EntityAlreadyExists

with DiagnosticCollector(prefix=["body"]) as diag:
    diag.append(EntityNotFound(loc=[1, "id"], id=1))
    diag.append(EntityAlreadyExists(loc=[2, "name"], entity="Hello"))

# Here, DiagnosticException is raised, containing both errors.

print(diag.errors)
# [
#     {
#         "loc": ["body", 1, "id"],
#         "msg": "Entity with id 1 not found",
#         "type": "entity_not_found",
#         "id": 1
#     },
#     {
#         "loc": ["body", 2, "name"],
#         "msg": "Entity already exists",
#         "type": "entity_already_exists",
#         "entity": "Hello"
#     }
# ]

Usage with FastAPI

from fastapi import FastAPI
from gcm_diagnostics import install_exception_handler, DiagnosticCollector
from gcm_diagnostics.errors import EntityNotFound
from starlette.testclient import TestClient

app = FastAPI()
install_exception_handler(app)

@app.get("/")
def diagnostics(id: int):
    with DiagnosticCollector(prefix=["query"]) as diag:
        diag.append(EntityNotFound(loc=["id"], id=id))


with TestClient(app, raise_server_exceptions=False) as client:
    response = client.get("/", params={"id": 123})
    assert response.status_code == 404
    assert response.json() == {
        "detail": [
            {
                "loc": ["query", "id"],
                "msg": "Entity does not exists.",
                "type": "entity_not_found",
                "id": 123
            }
        ]
    }

For more usage details, please consult the documentation in the code, mainly the documentation of class DiagnosticCollector.

Maturity

This library is currently used in various production applications and is considered stable.

Contributing

Contributions are always welcome. Just open an issue or a merge request.

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

gcm_diagnostics-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

gcm_diagnostics-1.0.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file gcm_diagnostics-1.0.0.tar.gz.

File metadata

  • Download URL: gcm_diagnostics-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/5.4.0-200-generic

File hashes

Hashes for gcm_diagnostics-1.0.0.tar.gz
Algorithm Hash digest
SHA256 06892d83284bc8418a9e2def625bbce6b4a6e344e6e37c754b54ed442b27c2d8
MD5 e39017ba1623575325ba5148c9b5be57
BLAKE2b-256 49844455a43a28faf5e54bbc1e43c0209c64d6b96d940b4f052efdf2ff6d7e3d

See more details on using hashes here.

File details

Details for the file gcm_diagnostics-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gcm_diagnostics-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/5.4.0-200-generic

File hashes

Hashes for gcm_diagnostics-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fd06da97039c31ffe877d0c0452c0c7551fb13913ee2f59a91e6a3ff825c62b
MD5 f2e440b3fa53ac2ea1e83e8500e861cb
BLAKE2b-256 986ddba2d8859b80d06a819835fde8d8e34035ebee8019f392918c8aeb952a28

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