Extract duplicate error handling patterns into reusable helpers#811
Merged
Extract duplicate error handling patterns into reusable helpers#811
Conversation
- Pattern 2 (High Priority): Extract SDK CallToolResult error returns - Created newErrorCallToolResult() helper in unified.go - Replaced 16 instances of duplicate error pattern - Added unit tests for the helper function - Pattern 1 (Medium Priority): Extract HTTP connection error checking - Created isHTTPConnectionError() helper in connection.go - Replaced 2 instances of duplicate error detection - Added unit tests for the helper function All existing tests pass. Total reduction: ~45 lines of duplicated code. Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
…ents - Replace assert.AnError with realistic fmt.Errorf() in tests - Add TODO comment acknowledging string-based error matching limitations - Suggest future improvement to use type assertions for error classification Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix duplicate code patterns in Go codebase
Extract duplicate error handling patterns into reusable helpers
Feb 7, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces duplicated error-handling logic by extracting repeated patterns into small helper functions in the server and MCP connection layers, and adds unit tests to cover the new helpers.
Changes:
- Added
newErrorCallToolResult(err)to standardize SDK tool error returns inUnifiedServer. - Added
isHTTPConnectionError(err)to centralize HTTP backend connection error classification. - Added unit tests covering both helpers.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/server/unified.go | Replaces repeated CallToolResult{IsError:true} error returns with newErrorCallToolResult helper. |
| internal/server/call_tool_result_test.go | Adds unit test for newErrorCallToolResult. |
| internal/mcp/connection.go | Introduces isHTTPConnectionError and uses it in HTTP init/request error handling. |
| internal/mcp/connection_test.go | Adds unit tests for isHTTPConnectionError. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+159
to
+171
| name string | ||
| err error | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "simple error", | ||
| err: fmt.Errorf("test error"), | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "formatted error", | ||
| err: fmt.Errorf("formatted error: %s", "test"), | ||
| expectError: true, |
There was a problem hiding this comment.
expectError in the test case struct is never used (all assertions are unconditional). This is misleading/unused state—either remove the field or use it to drive assertions (e.g., to support a future nil-error case).
Suggested change
| name string | |
| err error | |
| expectError bool | |
| }{ | |
| { | |
| name: "simple error", | |
| err: fmt.Errorf("test error"), | |
| expectError: true, | |
| }, | |
| { | |
| name: "formatted error", | |
| err: fmt.Errorf("formatted error: %s", "test"), | |
| expectError: true, | |
| name string | |
| err error | |
| }{ | |
| { | |
| name: "simple error", | |
| err: fmt.Errorf("test error"), | |
| }, | |
| { | |
| name: "formatted error", | |
| err: fmt.Errorf("formatted error: %s", "test"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Analysis identified 18 instances of duplicated error handling logic across
internal/server/unified.goandinternal/mcp/connection.go. This consolidates them into tested helper functions.Changes
SDK CallToolResult Error Returns (16 instances)
newErrorCallToolResult(err error) (*sdk.CallToolResult, interface{}, error)helperreturn &sdk.CallToolResult{IsError: true}, nil, errHTTP Connection Error Detection (2 instances)
isHTTPConnectionError(err error) boolhelperTest Coverage
Notes
*net.OpErrortype assertionsWarning
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/tmp/go-build3296312196/b271/launcher.test /tmp/go-build3296312196/b271/launcher.test -test.testlogfile=/tmp/go-build3296312196/b271/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true ache/go/1.25.6/x/tmp/tmp.7JmQLDmIdb/golangci-lint-2.8.0-linux-amd64.tar.gz nto helper functREDACTED x_amd64/vet -pthread -Wl,--no-gc-sect-o -fmessage-length/tmp/go-build1663340372/b266/_pkg_.a x_amd64/vet -I ache/go/1.25.6/x-p -fPIC x_amd64/vet -pthread -Wl,--no-gc-sect-o -fmessage-length/tmp/go-build3296312196/b259/_pkg_.a x_amd64/vet(dns block)/tmp/go-build3756687988/b271/launcher.test /tmp/go-build3756687988/b271/launcher.test -test.testlogfile=/tmp/go-build3756687988/b271/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true(dns block)invalid-host-that-does-not-exist-12345.comOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.