Skip to content

Add automatic problem creation for API errors#5

Merged
nlr-ai merged 1 commit intomainfrom
api-problems
Jun 27, 2025
Merged

Add automatic problem creation for API errors#5
nlr-ai merged 1 commit intomainfrom
api-problems

Conversation

@nlr-ai
Copy link
Copy Markdown
Contributor

@nlr-ai nlr-ai commented Jun 27, 2025

Summary

Extends the self-healing infrastructure to the FastAPI server by automatically creating problem records when API endpoints encounter unhandled errors.

What's New

Global Exception Handler

A comprehensive exception handler that:

  • Catches all unhandled exceptions (except validation errors)
  • Creates detailed problem records in the PROBLEMS table
  • Logs full error details and tracebacks
  • Optionally sends Telegram notifications

Request Body Middleware

  • Captures JSON request bodies for error debugging
  • Safely handles different content types
  • Avoids issues with file uploads

Problem Records Include:

  • Unique Problem ID: api_error_YYYYMMDD_HHMMSS_endpoint_path
  • Type: api_endpoint_error
  • Severity: High for 500 errors, Medium for others
  • Description: Full error details including:
    • Endpoint path and HTTP method
    • Error type and message
    • Request data (JSON only, truncated if large)
    • Full traceback (truncated to 1000 chars)
  • Suggested Solutions: Common fixes for API issues
  • Assignment: ConsiglioDeiDieci (system problems)

Error Types Handled:

  • ✅ Database connection failures
  • ✅ Missing environment variables
  • ✅ Import errors
  • ✅ Airtable API errors
  • ✅ Any unhandled exception

Duplicate Prevention

  • Checks for similar problems in the last hour
  • Prevents spam when an endpoint repeatedly fails

Integration with Arsenale

With this change, Arsenale can now detect and fix API errors:

  1. OBSERVE: Find API error problems in PROBLEMS table
  2. ASSESS: Analyze error type and traceback
  3. EXECUTE: Fix the issue (add missing imports, set env vars, etc.)
  4. DOCUMENT: Verify the API is working again

Example Problem Record

{
  "ProblemId": "api_error_20250627_1045_api_wallet",
  "Type": "api_endpoint_error",
  "Title": "API Error: /api/wallet (POST)",
  "Description": "API endpoint '/api/wallet' (POST) encountered an error.\n\nError Type: KeyError\nError Message: 'AIRTABLE_API_KEY'\n\nRequest Data:\n{\n  \"wallet_address\": \"abc123\",\n  \"ducats\": 100\n}\n\nTraceback:\n...",
  "Status": "active",
  "Severity": "High",
  "Solutions": ["Check the error message...", "Verify all required environment variables..."]
}

Complete Self-Healing Loop

La Serenissima now has automatic problem creation for:

  • ✅ Scheduled task failures (scheduler.py)
  • ✅ API endpoint failures (main.py)
  • ✅ Manual problem detection scripts

This creates a complete feedback loop where infrastructure issues are automatically detected, logged, and made available for Arsenale to fix autonomously!


🤖 Generated with Claude Code

Implements global exception handler for the FastAPI server that automatically
creates problem records when API endpoints fail:

- Global exception handler catches all unhandled exceptions
- Creates problems with endpoint, method, error details, and traceback
- Includes request data when available (JSON requests only)
- Prevents duplicate problems within 1 hour window
- Optional Telegram notifications for critical errors
- Request body middleware to capture JSON payloads for debugging

This enables Arsenale to detect and fix API failures autonomously,
completing the self-healing infrastructure for all backend services.

Problem records include:
- Unique problem ID with timestamp and endpoint
- Error type, message, and full traceback
- Request data (truncated if too large)
- Suggested solutions for common issues
- Assignment to ConsiglioDeiDieci as system problems

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@nlr-ai nlr-ai merged commit 9c1dd7f into main Jun 27, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant