{#- Error Handling State Machine Template -#}
    // Error Handling State Machine
    typedef enum logic [2:0] {
        ERR_NORMAL      = 3'b000,
        ERR_DETECTED    = 3'b001,
        ERR_ANALYZING   = 3'b010,
        ERR_RECOVERING  = 3'b011,
        ERR_RETRY       = 3'b100,
        ERR_FATAL       = 3'b101,
        ERR_LOGGING     = 3'b110
    } error_state_t;

    error_state_t error_state = ERR_NORMAL;
    error_state_t error_next_state;

    // Error state machine logic
    always_ff @(posedge clk or negedge reset_n) begin
        if (!reset_n) begin
            error_state <= ERR_NORMAL;
            retry_count <= 4'h0;
            error_recovery_timer <= 16'h0;
            error_recovery_active <= 1'b0;
        end else begin
            error_state <= error_next_state;
            
            if (error_state != ERR_NORMAL) begin
                error_recovery_timer <= error_recovery_timer + 1;
                error_recovery_active <= 1'b1;
            end else begin
                error_recovery_timer <= 16'h0;
                error_recovery_active <= 1'b0;
            end
        end
    end

    {# Provide safe defaults if error_config or fields are missing in the template context #}
    {%- set max_retry_count = error_config.max_retry_count if (error_config is defined and error_config.max_retry_count is defined) else 3 -%}
    {%- set error_recovery_cycles = error_config.error_recovery_cycles if (error_config is defined and error_config.error_recovery_cycles is defined) else 16 -%}
    {%- set enable_error_logging = error_config.enable_error_logging if (error_config is defined and error_config.enable_error_logging is defined) else false -%}

    // Error state combinational logic
    always_comb begin
        error_next_state = error_state;
        
        case (error_state)
            ERR_NORMAL: begin
                if (parity_error || crc_error || timeout_error || ecc_error) begin
                    error_next_state = ERR_DETECTED;
                end
            end
            
            ERR_DETECTED: begin
                error_next_state = ERR_ANALYZING;
            end
            
            ERR_ANALYZING: begin
                if (error_recovery_timer >= {{ error_recovery_cycles }}) begin
                    if (timeout_error) begin
                        error_next_state = ERR_FATAL;  // Timeout is fatal
                    end else if (retry_count < {{ max_retry_count }}) begin
                        error_next_state = ERR_RETRY;
                    end else begin
                        error_next_state = ERR_FATAL;
                    end
                end
            end
            
            ERR_RETRY: begin
{%- if enable_error_logging %}
                error_next_state = ERR_LOGGING;
{%- else %}
                error_next_state = ERR_NORMAL;
{%- endif %}
            end
            
{%- if enable_error_logging %}
            ERR_LOGGING: begin
                error_next_state = ERR_NORMAL;
            end
            
{%- endif %}
            ERR_FATAL: begin
                // Stay in fatal state until reset
            end
            
            default: error_next_state = ERR_NORMAL;
        endcase
    end