Skip to content

Commit 2824cb2

Browse files
tiwilliaclaude
andcommitted
Refactor error handling in MCP tools to reduce code duplication
Extract duplicated OCM error handling logic into a reusable utility function handleOCMError(). This consolidates 36 lines of repeated error handling across 4 tool handlers into a single 14-line utility function, improving code maintainability and consistency. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 38995e9 commit 2824cb2

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

pkg/mcp/tools.go

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,8 @@ func (s *Server) handleWhoami(ctx context.Context, ctr mcp.CallToolRequest) (*mc
6868

6969
// Call OCM client to get current account
7070
account, err := client.GetCurrentAccount()
71-
if err != nil {
72-
// Handle OCM API errors with enhanced token expiration detection
73-
if ocmErr, ok := err.(*ocm.OCMError); ok {
74-
// Return specific error for token expiration
75-
if ocm.IsAccessTokenExpiredError(ocmErr) {
76-
return mcp.NewToolResultError("AUTHENTICATION_FAILED: " + ocmErr.Error()), nil
77-
}
78-
return NewTextResult("", errors.New("OCM API Error ["+ocmErr.Code+"]: "+ocmErr.Reason)), nil
79-
}
80-
return NewTextResult("", errors.New("failed to get account: "+err.Error())), nil
71+
if errorResult := handleOCMError(err, "failed to get account"); errorResult != nil {
72+
return errorResult, nil
8173
}
8274

8375
// Format response using MCP layer formatter
@@ -105,16 +97,8 @@ func (s *Server) handleGetClusters(ctx context.Context, ctr mcp.CallToolRequest)
10597

10698
// Call OCM client to get clusters with state filter
10799
clusters, err := client.GetClusters(state)
108-
if err != nil {
109-
// Handle OCM API errors with enhanced token expiration detection
110-
if ocmErr, ok := err.(*ocm.OCMError); ok {
111-
// Return specific error for token expiration
112-
if ocm.IsAccessTokenExpiredError(ocmErr) {
113-
return mcp.NewToolResultError("AUTHENTICATION_FAILED: " + ocmErr.Error()), nil
114-
}
115-
return NewTextResult("", errors.New("OCM API Error ["+ocmErr.Code+"]: "+ocmErr.Reason)), nil
116-
}
117-
return NewTextResult("", errors.New("failed to get clusters: "+err.Error())), nil
100+
if errorResult := handleOCMError(err, "failed to get clusters"); errorResult != nil {
101+
return errorResult, nil
118102
}
119103

120104
// Format response using MCP layer formatter
@@ -142,16 +126,8 @@ func (s *Server) handleGetCluster(ctx context.Context, ctr mcp.CallToolRequest)
142126

143127
// Call OCM client to get cluster details
144128
cluster, err := client.GetCluster(clusterID)
145-
if err != nil {
146-
// Handle OCM API errors with enhanced token expiration detection
147-
if ocmErr, ok := err.(*ocm.OCMError); ok {
148-
// Return specific error for token expiration
149-
if ocm.IsAccessTokenExpiredError(ocmErr) {
150-
return mcp.NewToolResultError("AUTHENTICATION_FAILED: " + ocmErr.Error()), nil
151-
}
152-
return NewTextResult("", errors.New("OCM API Error ["+ocmErr.Code+"]: "+ocmErr.Reason)), nil
153-
}
154-
return NewTextResult("", errors.New("failed to get cluster: "+err.Error())), nil
129+
if errorResult := handleOCMError(err, "failed to get cluster"); errorResult != nil {
130+
return errorResult, nil
155131
}
156132

157133
// Format response using MCP layer formatter
@@ -271,16 +247,8 @@ func (s *Server) handleCreateROSAHCPCluster(ctx context.Context, ctr mcp.CallToo
271247
subnetIDs, availabilityZones, region,
272248
multiArchEnabled,
273249
)
274-
if err != nil {
275-
// Handle OCM API errors with enhanced token expiration detection
276-
if ocmErr, ok := err.(*ocm.OCMError); ok {
277-
// Return specific error for token expiration
278-
if ocm.IsAccessTokenExpiredError(ocmErr) {
279-
return mcp.NewToolResultError("AUTHENTICATION_FAILED: " + ocmErr.Error()), nil
280-
}
281-
return NewTextResult("", errors.New("OCM API Error ["+ocmErr.Code+"]: "+ocmErr.Reason)), nil
282-
}
283-
return NewTextResult("", errors.New("cluster creation failed: "+err.Error())), nil
250+
if errorResult := handleOCMError(err, "cluster creation"); errorResult != nil {
251+
return errorResult, nil
284252
}
285253

286254
// Format response using MCP layer formatter
@@ -310,3 +278,21 @@ func NewTextResult(content string, err error) *mcp.CallToolResult {
310278
},
311279
}
312280
}
281+
282+
// handleOCMError processes OCM API errors with enhanced token expiration detection
283+
// Returns an appropriate MCP CallToolResult for the error, or nil if no error
284+
func handleOCMError(err error, operation string) *mcp.CallToolResult {
285+
if err == nil {
286+
return nil
287+
}
288+
289+
// Handle OCM API errors with enhanced token expiration detection
290+
if ocmErr, ok := err.(*ocm.OCMError); ok {
291+
// Return specific error for token expiration
292+
if ocm.IsAccessTokenExpiredError(ocmErr) {
293+
return mcp.NewToolResultError("AUTHENTICATION_FAILED: " + ocmErr.Error())
294+
}
295+
return NewTextResult("", errors.New("OCM API Error ["+ocmErr.Code+"]: "+ocmErr.Reason))
296+
}
297+
return NewTextResult("", errors.New(operation+" failed: "+err.Error()))
298+
}

0 commit comments

Comments
 (0)