waddling-errors-macros 0.7.3

Procedural macros for structured error codes with compile-time validation and taxonomy enforcement
Documentation
//! Database Component
//!
//! PostgreSQL database operations - connection pooling, queries, transactions, data integrity.
//!
//! This version uses the `component!` and `diag!` macros from waddling-errors-macros.

use waddling_errors_macros::{component, component_location, diag};

// Mark this file as Database component - internal only (sensitive DB details)
component_location!(Database, role = internal);

component! {
    Database {
        docs: "PostgreSQL database operations. Covers connection pooling, query execution, transactions, and data integrity checks.",
        examples: [
            "E.Db.Connection.NOT_FOUND: Database connection pool exhausted",
            "C.Db.Data.CORRUPTED: Foreign key constraint violation detected",
            "E.Db.Timeout.EXCEEDED: Query execution timeout after 30 seconds"
        ],
        tags: ["persistence", "sql", "transactions"],
    },
}

// ============================================================================
// Error Code Definitions
// ============================================================================

diag! {
    <json, html>,


    E.Db.Connection.NOT_FOUND: {
        message: "Database connection not found in pool",
        'CR 'Pub description: "The requested database connection could not be found in the connection pool. This may indicate the connection was closed or the pool is not initialized.",
        'CR 'Pub hints: [
            "Check DATABASE_URL",
            "Verify DB server is running"
        ],
        'CR 'Int hints: [
            "Review connection pool configuration",
            "Check for connection leaks"
        ],
        'R role: "Internal",
        'R tags: ["database", "connectivity"],
        'R related_codes: ["C.Db.Connection.EXHAUSTED"],
    },

    C.Db.Connection.EXHAUSTED: {
        message: "CRITICAL: Connection pool exhausted",
        'CR 'Pub description: "The database connection pool has no available connections. This is a critical issue that can cause cascading failures and service degradation.",
        'CR 'Pub hints: [
            "Scale up immediately",
            "Investigate long queries",
            "Enable read replicas"
        ],
        'CR 'Int hints: [
            "Monitor connection pool metrics",
            "Review slow query log",
            "Check for connection leaks in application code"
        ],
        'R role: "Internal",
        'R tags: ["database", "performance", "critical"],
        'R related_codes: ["E.Db.Connection.NOT_FOUND", "E.Db.Timeout.EXCEEDED"],
    },

    E.Db.Timeout.EXCEEDED: {
        message: "Database query timeout exceeded",
        'CR 'Pub description: "The database query took longer than the configured timeout limit. This may indicate missing indexes, inefficient queries, or database overload.",
        'CR 'Pub hints: [
            "Optimize query with indexes",
            "Increase timeout limit",
            "Add query cache"
        ],
        'CR 'Int hints: [
            "Run EXPLAIN ANALYZE on slow queries",
            "Check for table locks",
            "Review query execution plan"
        ],
        'R role: "Internal",
        'R tags: ["database", "performance"],
        'R related_codes: ["C.Db.Connection.EXHAUSTED"],
    },

    C.Db.Data.CORRUPTED: {
        message: "CRITICAL: Data corruption detected",
        'CR 'Pub description: "Database data corruption has been detected. This is a critical issue requiring immediate attention to prevent data loss.",
        'CR 'Pub hints: [
            "Backup immediately",
            "Run VACUUM FULL",
            "Check hardware"
        ],
        'CR 'Int hints: [
            "Check PostgreSQL logs for I/O errors",
            "Verify disk integrity with fsck",
            "Review replication lag if using replicas"
        ],
        'R role: "Internal",
        'R tags: ["database", "integrity", "critical"],
        'R related_codes: ["E.Db.Data.CONFLICT", "C.Storage.Data.CORRUPTED"],
    },

    E.Db.Data.CONFLICT: {
        message: "Data conflict - concurrent modification detected",
        'CR 'Pub description: "A concurrent modification conflict was detected. Another transaction modified the same data between read and write operations.",
        'CR 'Pub hints: [
            "Retry with latest version",
            "Implement optimistic locking"
        ],
        'CR 'Dev hints: [
            "Use SELECT FOR UPDATE for pessimistic locking",
            "Implement version-based conflict resolution"
        ],
        'R role: "Developer",
        'R tags: ["database", "concurrency"],
    },

    E.Db.Validation.INVALID: {
        message: "Database constraint validation failed",
        'CR 'Pub description: "A database constraint (foreign key, unique, check) validation failed. The data does not meet the database schema requirements.",
        'CR 'Pub hints: [
            "Check foreign key references",
            "Verify unique constraints"
        ],
        'CR 'Dev hints: [
            "Review constraint definitions in schema",
            "Validate data before insertion"
        ],
        'R role: "Developer",
        'R tags: ["database", "validation"],
    },
}