Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 7, 2026

Duplicate string matching logic for detecting connection errors (connection refused, no such host, network is unreachable) existed in both initializeHTTPSession and sendHTTPRequest.

Changes

  • Added isConnectionError() helper: Centralizes connection error detection logic
  • Updated two call sites: Both methods now use the helper instead of inline checks

Before

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)
}

After

if isConnectionError(err) {
    return nil, fmt.Errorf("cannot connect to HTTP backend at %s: %w", c.httpURL, err)
}

Adding new error types now requires updating a single location.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build373359847/b271/launcher.test /tmp/go-build373359847/b271/launcher.test -test.testlogfile=/tmp/go-build373359847/b271/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go path/match.go x_amd64/vet (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build373359847/b259/config.test /tmp/go-build373359847/b259/config.test -test.testlogfile=/tmp/go-build373359847/b259/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --global go 64/pkg/tool/linux_amd64/compile (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build373359847/b271/launcher.test /tmp/go-build373359847/b271/launcher.test -test.testlogfile=/tmp/go-build373359847/b271/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go path/match.go x_amd64/vet (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build373359847/b271/launcher.test /tmp/go-build373359847/b271/launcher.test -test.testlogfile=/tmp/go-build373359847/b271/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go path/match.go x_amd64/vet (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build373359847/b280/mcp.test /tmp/go-build373359847/b280/mcp.test -test.testlogfile=/tmp/go-build373359847/b280/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true go --global /home/REDACTED/.ca-o user.name (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate Code Pattern: HTTP Connection Error Checking</issue_title>
<issue_description># 🔍 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

Comments on the Issue (you are @copilot in this section)

Custom agent used: agentic-workflows
GitHub Agentic Workflows (gh-aw) - Create, debug, and upgrade AI-powered workflows with intelligent prompt routing


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…r function

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated HTTP connection error checking logic Extract duplicate HTTP connection error detection into helper function Feb 7, 2026
Copilot AI requested a review from lpcox February 7, 2026 01:32
@lpcox lpcox marked this pull request as ready for review February 7, 2026 03:58
Copilot AI review requested due to automatic review settings February 7, 2026 03:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces duplication in the MCP HTTP connection logic by extracting repeated “connection error” string-matching checks into a single helper, improving maintainability and consistency in error handling.

Changes:

  • Added an isConnectionError(err error) bool helper in internal/mcp/connection.go.
  • Updated initializeHTTPSession and sendHTTPRequest to use the helper.
  • Added unit tests covering isConnectionError behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/mcp/connection.go Introduces isConnectionError and replaces duplicated inline checks at two HTTP call sites.
internal/mcp/connection_test.go Adds table-driven tests for the new helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

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

2 participants