{#- Error Handling Logic Template -#}
    // Error Handling State Machine
    typedef enum logic [2:0] {
        ERR_IDLE           = 3'b000,
        ERR_DETECTED       = 3'b001,
        ERR_CORRECTING     = 3'b010,
        ERR_RECOVERY       = 3'b011,
        ERR_FATAL          = 3'b100,
        ERR_DISABLED       = 3'b101
    } error_state_t;

    error_state_t error_state = ERR_IDLE;
    error_state_t error_next_state;

    // Error detection signals
    logic correctable_error_detected;
    logic uncorrectable_error_detected;
    logic fatal_error_detected;
    logic [3:0] retry_count = 4'h0;

    // Error handling logic
    always_ff @(posedge clk or negedge reset_n) begin
        if (!reset_n) begin
            error_state <= ERR_IDLE;
            error_recovery_timer <= 16'h0;
            error_recovery_active <= 1'b0;
            retry_count <= 4'h0;
            correctable_error_count <= 8'h0;
            uncorrectable_error_count <= 8'h0;
        end else begin
            error_state <= error_next_state;
            
            // Update error counters
            if (correctable_error_detected) begin
                correctable_error_count <= correctable_error_count + 1;
            end
            
            if (uncorrectable_error_detected) begin
                uncorrectable_error_count <= uncorrectable_error_count + 1;
            end
            
            // Error recovery timer
            if (error_recovery_active) begin
                error_recovery_timer <= error_recovery_timer + 1;
            end else begin
                error_recovery_timer <= 16'h0;
            end
        end
    end

    // Error state machine
    always_comb begin
        error_next_state = error_state;
        error_recovery_active = 1'b0;
        
        case (error_state)
            ERR_IDLE: begin
                if (fatal_error_detected) begin
                    error_next_state = ERR_FATAL;
                end else if (uncorrectable_error_detected) begin
                    error_next_state = ERR_RECOVERY;
                end else if (correctable_error_detected) begin
                    error_next_state = ERR_CORRECTING;
                end
            end
            
            ERR_CORRECTING: begin
                // Attempt automatic correction
                error_recovery_active = 1'b1;
                if (error_recovery_timer >= {{ error_handling.recovery_cycles }}) begin
                    error_next_state = ERR_IDLE;
                end
            end
            
            ERR_RECOVERY: begin
                error_recovery_active = 1'b1;
                if (error_recovery_timer >= {{ error_handling.recovery_cycles }}) begin
                    if (retry_count < {{ error_handling.max_retry_count }}) begin
                        error_next_state = ERR_IDLE;
                    end else begin
                        error_next_state = ERR_FATAL;
                    end
                end
            end
            
            ERR_FATAL: begin
                // Fatal error - device disabled
                error_next_state = ERR_DISABLED;
            end
            
            ERR_DISABLED: begin
                // Device remains disabled until reset
                error_next_state = ERR_DISABLED;
            end
            
            default: error_next_state = ERR_IDLE;
        endcase
    end

    // Error detection logic
    always_comb begin
        correctable_error_detected = 1'b0;
        uncorrectable_error_detected = 1'b0;
        fatal_error_detected = 1'b0;
        
        // Check for various error conditions
        if (parity_error || crc_error) begin
            correctable_error_detected = 1'b1;
        end
        
        if (timeout_error || protocol_error) begin
            uncorrectable_error_detected = 1'b1;
        end
        
        if (system_error || internal_error) begin
            fatal_error_detected = 1'b1;
        end
    end

    // Error status outputs
    assign error_status = {
        fatal_error_detected,
        uncorrectable_error_detected,
        correctable_error_detected,
        error_recovery_active,
        error_state[2:0],
        1'b0
    };