Skip to main content

Transport-agnostic, typed error DTOs and envelopes for APIs

Project description

apierrors

Transport-agnostic, typed error DTOs and envelopes for APIs. Use the same domain error objects across HTTP, gRPC, GraphQL, WebSocket, JSON-RPC, and message queues—with first-class support for HTTP out of the box and a 422 format compatible with FastAPI.


Why

Framework built-ins (e.g., HTTPException) are convenient but hard-wire errors to one transport. apierrors keeps your domain errors independent and adds thin adapters per transport. Your handlers build rich, typed errors; adapters serialize them for the wire.


Features

  • Typed DTOs: Err400/401/403/404/405/409/422/... with critical fields (e.g., loc for 422, allowed_methods for 405, permission for 403).
  • Transport-agnostic core: domain errors don’t depend on HTTP.
  • HTTP envelopes: HttpErrorEnvelope[T] with status_code, optional headers.
  • FastAPI-style 422: Err422 supports loc, error_type (aka type), ctx, message (aka msg).
  • Consistent JSON: stable shape for clients & SDKs.
  • Headers helpers: easy WWW-Authenticate, Allow, Retry-After.
  • Adapters (extensible): examples for gRPC, GraphQL, WS.

Install

pip install apierrors

Python 3.11+.


Why not just HTTPException (FastAPI / Starlette)?

Aspect HTTPException apierrors
Transport HTTP-only Transport-agnostic domain errors + HTTP adapter
Typing/DTOs Message + status Rich typed DTOs per status (401, 403, 405, 409, 422…)
422 details Framework-controlled Explicit loc/error_type/ctx, same across transports
Headers Manual, ad-hoc DTO fields + helper factories (e.g., Allow, WWW-Authenticate)
Reuse in gRPC/GraphQL/WS No Yes, via adapters
Client SDKs Ad-hoc parsing Stable JSON shapes for codegen/SDKs
Testability Inspect exception Assert on DTO fields & envelopes

Bottom line: keep domain errors independent; adapt once per transport.


Comparison to other libs

  • Starlette / FastAPI exceptions: great for quick HTTP flows but hard-wire transport and shape; 422 format depends on framework internals. apierrors gives you the same clarity for 422 while remaining cross-transport.
  • DRF APIException / Marshmallow errors: framework-centric, tightly coupled to serializers/validators. apierrors can ingest their outputs and produce a consistent cross-transport error shape.
  • Werkzeug/HTTPX exceptions: HTTP-only and less prescriptive about rich detail.

Best practices

  • Build domain errors, not responses. Keep HTTP concerns at the edge.
  • Use specific DTOs. Prefer Err403(resource=..., action=..., permission=...) over a generic message.
  • 422 consistently. Use loc paths identical to your validator (("body","items",0,"price")).
  • Set headers via envelope. Mirror DTO fields into headers (e.g., Allow).
  • Log request_id. Populate for traceability; ship timestamp automatically.

Roadmap

  • More transports & adapters
    • gRPC error details (google.rpc.BadRequest / ErrorInfo)
    • GraphQL extensions & Apollo format
    • JSON-RPC 2.0 (code/message/data)
    • WebSocket/SSE payloads & close codes
    • Message buses (Kafka/RabbitMQ) error envelopes
  • RFC7807 adapter (Problem Details) with per-status extensions
  • Pydantic & Marshmallow bridges (auto-convert validation errors → Err422)
  • i18n: message translation hooks
  • Retry semantics: helpers for Retry-After, backoff hints
  • Codegen: JSON Schema for errors to power SDKs
  • 5xx suite: Err500..Err504 with incident/trace fields

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

apierrors-0.0.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

apierrors-0.0.1-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file apierrors-0.0.1.tar.gz.

File metadata

  • Download URL: apierrors-0.0.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for apierrors-0.0.1.tar.gz
Algorithm Hash digest
SHA256 730751f6c001da37b193e9bb65f6b2fc5d0c11f807a28d11ded1c871b66de7a0
MD5 e53a3631d3eee06703dfdc0f8c2c1b72
BLAKE2b-256 41263c0f44bd730f7edfc356a26636066e9fce9edb890ef7bb204eda72d44e1f

See more details on using hashes here.

File details

Details for the file apierrors-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: apierrors-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for apierrors-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba0809a6e97c6918ab0b0e1a160c8b16513e7169d9704ca876cffe2d057903f8
MD5 5561a7a94e36d2a2ded2d1f5fd86b38a
BLAKE2b-256 ecd9169ba5f7ff7ec4c832a9c1bc0f76312e058a007fb01c85d5ac7aea2d554e

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