Skip to main content

Error Explorer SDK for FastAPI

Project description

Error Explorer FastAPI SDK

Official FastAPI integration for Error Explorer error tracking.

Installation

pip install error-explorer-fastapi

Quick Start

from fastapi import FastAPI
from error_explorer import ErrorExplorer
from error_explorer_fastapi import ErrorExplorerMiddleware

# Initialize Error Explorer
ErrorExplorer.init({
    "token": "your-project-token",
    "environment": "production",
})

# Create FastAPI app with middleware
app = FastAPI()
app.add_middleware(ErrorExplorerMiddleware)

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

Using setup_error_explorer

For convenience, you can use the setup function:

from fastapi import FastAPI
from error_explorer import ErrorExplorer
from error_explorer_fastapi.middleware import setup_error_explorer

app = FastAPI()
ErrorExplorer.init({"token": "your-token"})
setup_error_explorer(app)

Configuration

The middleware accepts several configuration options:

app.add_middleware(
    ErrorExplorerMiddleware,
    # Capture user info from request.state.user
    capture_user=True,
    # Send PII (email, IP address)
    send_default_pii=False,
    # Capture 404 errors
    capture_404=False,
    # Capture 403 errors
    capture_403=False,
)

Features

Automatic Error Capture

All unhandled exceptions are automatically captured:

@app.get("/error")
async def trigger_error():
    raise ValueError("Something went wrong")
    # This is automatically captured

Request Breadcrumbs

HTTP requests are logged as breadcrumbs:

→ GET /api/users
← 200 OK

User Context

Set user context via request state (typically done by auth middleware):

@app.middleware("http")
async def auth_middleware(request: Request, call_next):
    user = await get_current_user(request)
    request.state.user = user  # User context captured automatically
    return await call_next(request)

Logging Handler

Use the logging handler to capture log messages:

import logging
from error_explorer_fastapi import ErrorExplorerHandler

# Add handler to root logger
handler = ErrorExplorerHandler(level=logging.WARNING)
logging.getLogger().addHandler(handler)

# Now WARNING and above are captured
logging.warning("This will be a breadcrumb")
logging.error("This will be captured as an event")

Manual Error Capture

from error_explorer import ErrorExplorer

@app.get("/process")
async def process():
    try:
        await risky_operation()
    except Exception as e:
        ErrorExplorer.get_client().capture_exception(e)
        raise HTTPException(status_code=500, detail="Error occurred")

Adding Context

from error_explorer import ErrorExplorer, Breadcrumb

@app.get("/checkout")
async def checkout():
    client = ErrorExplorer.get_client()

    # Add custom breadcrumb
    client.add_breadcrumb(Breadcrumb(
        message="User started checkout",
        category="checkout",
        data={"cart_items": 5}
    ))

    # Set additional context
    client.set_context("order", {
        "total": 99.99,
        "currency": "USD"
    })

    return await process_checkout()

Async Support

The middleware is fully async-compatible:

@app.get("/async-operation")
async def async_operation():
    result = await some_async_function()
    return {"result": result}

License

MIT

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

error_explorer_fastapi-1.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

error_explorer_fastapi-1.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file error_explorer_fastapi-1.1.0.tar.gz.

File metadata

  • Download URL: error_explorer_fastapi-1.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for error_explorer_fastapi-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f5a42cf16d52ae7d4c979bb1b7a21068512b5be64780c85bc6051be04edafd16
MD5 f81dae03e257c58f4aeb55eb2b7e67a8
BLAKE2b-256 ff6d0e0d4375f9f60c51b533a8f8954150cdf44424dcd3ea439be8f040f74097

See more details on using hashes here.

File details

Details for the file error_explorer_fastapi-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for error_explorer_fastapi-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 099eacd21b8bdf325a6acf277467754a52192994dabd0254e741779005f13904
MD5 1c184ccb16082a242bd15a81ca7e4b91
BLAKE2b-256 486218e8b3baee584bc059548993a86fad184908ffa305e3dc66680266fa5559

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