@@ -68,16 +68,8 @@ func (s *Server) handleWhoami(ctx context.Context, ctr mcp.CallToolRequest) (*mc
68
68
69
69
// Call OCM client to get current account
70
70
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
81
73
}
82
74
83
75
// Format response using MCP layer formatter
@@ -105,16 +97,8 @@ func (s *Server) handleGetClusters(ctx context.Context, ctr mcp.CallToolRequest)
105
97
106
98
// Call OCM client to get clusters with state filter
107
99
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
118
102
}
119
103
120
104
// Format response using MCP layer formatter
@@ -142,16 +126,8 @@ func (s *Server) handleGetCluster(ctx context.Context, ctr mcp.CallToolRequest)
142
126
143
127
// Call OCM client to get cluster details
144
128
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
155
131
}
156
132
157
133
// Format response using MCP layer formatter
@@ -271,16 +247,8 @@ func (s *Server) handleCreateROSAHCPCluster(ctx context.Context, ctr mcp.CallToo
271
247
subnetIDs , availabilityZones , region ,
272
248
multiArchEnabled ,
273
249
)
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
284
252
}
285
253
286
254
// Format response using MCP layer formatter
@@ -310,3 +278,21 @@ func NewTextResult(content string, err error) *mcp.CallToolResult {
310
278
},
311
279
}
312
280
}
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