Skip to content

[duplicate-code] Duplicate Code Pattern: HTTP Connection Error Checking #740

@github-actions

Description

@github-actions

🔍 Duplicate Code Pattern: HTTP Connection Error Checking

Part of duplicate code analysis: #739

Summary

Duplicated HTTP connection error detection logic in internal/mcp/connection.go. The same error checking pattern (connection refused, no such host, network unreachable) appears in two methods with identical string matching logic.

Duplication Details

Pattern: HTTP Connection Error Detection

  • Severity: Medium

  • Occurrences: 2 instances in the same file

  • Locations:

    • internal/mcp/connection.go (lines 587-590) - in initializeHTTPSession
    • internal/mcp/connection.go (lines 701-704) - in sendHTTPRequest
  • Code Sample:

    // Duplicated in initializeHTTPSession (lines 587-590)
    if strings.Contains(err.Error(), "connection refused") ||
        strings.Contains(err.Error(), "no such host") ||
        strings.Contains(err.Error(), "network is unreachable") {
        return "", fmt.Errorf("cannot connect to HTTP backend at %s: %w", c.httpURL, err)
    }
    
    // Duplicated in sendHTTPRequest (lines 701-704)
    if strings.Contains(err.Error(), "connection refused") ||
        strings.Contains(err.Error(), "no such host") ||
        strings.Contains(err.Error(), "network is unreachable") {
        return nil, fmt.Errorf("cannot connect to HTTP backend at %s: %w", c.httpURL, err)
    }

Impact Analysis

  • Maintainability: Adding new error types requires updating multiple locations
  • Bug Risk: Inconsistent error handling if one location is updated but not the other
  • Code Bloat: ~8 lines of duplicated logic

Refactoring Recommendations

  1. Extract to Helper Function

    • Extract to: internal/mcp/connection.go (add helper method)
    • Estimated effort: 15 minutes
    • Benefits: Single source of truth for connection error detection
    // isConnectionError checks if an error indicates a connection failure
    func isConnectionError(err error) bool {
        if err == nil {
            return false
        }
        errStr := err.Error()
        return strings.Contains(errStr, "connection refused") ||
               strings.Contains(errStr, "no such host") ||
               strings.Contains(errStr, "network is unreachable")
    }
    
    // Usage in both methods:
    if isConnectionError(err) {
        return nil, fmt.Errorf("cannot connect to HTTP backend at %s: %w", c.httpURL, err)
    }

Implementation Checklist

  • Review duplication findings
  • Create isConnectionError helper function in internal/mcp/connection.go
  • Replace both duplicated checks with helper calls
  • Run make test-all to verify no functionality broken
  • Consider adding tests for the new helper function
  • Run make agent-finished before completion

Parent Issue

See parent analysis report: #739
Related to #739

AI generated by Duplicate Code Detector

  • expires on Feb 13, 2026, 3:07 AM UTC

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions