-
Notifications
You must be signed in to change notification settings - Fork 7
Closed as not planned
Closed as not planned
Copy link
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) - ininitializeHTTPSessioninternal/mcp/connection.go(lines 701-704) - insendHTTPRequest
-
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
-
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) }
- Extract to:
Implementation Checklist
- Review duplication findings
- Create
isConnectionErrorhelper function ininternal/mcp/connection.go - Replace both duplicated checks with helper calls
- Run
make test-allto verify no functionality broken - Consider adding tests for the new helper function
- Run
make agent-finishedbefore 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
Reactions are currently unavailable