Skip to content

Commit 7adfdfc

Browse files
committed
Use concrete error types
1 parent 7e026fc commit 7adfdfc

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

pkg/github/errors.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package github
2+
3+
import "fmt"
4+
5+
type MissingRequiredParameterError struct {
6+
Parameter string
7+
}
8+
9+
// Error implements the error interface for MissingRequiredParameterError.
10+
func (e MissingRequiredParameterError) Error() string {
11+
return fmt.Sprintf("missing required parameter: %s", e.Parameter)
12+
}
13+
14+
type InvalidParameterTypeError struct {
15+
Parameter string
16+
Expected string
17+
Actual string
18+
}
19+
20+
// Error implements the error interface for InvalidParameterTypeError.
21+
func (e InvalidParameterTypeError) Error() string {
22+
return fmt.Sprintf("parameter %s is not of type %s, is %s", e.Parameter, e.Expected, e.Actual)
23+
}

pkg/github/server.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@ func requiredParam[T comparable](r mcp.CallToolRequest, p string) (T, error) {
6969

7070
// Check if the parameter is present in the request
7171
if _, ok := r.GetArguments()[p]; !ok {
72-
return zero, fmt.Errorf("missing required parameter: %s", p)
72+
return zero, MissingRequiredParameterError{Parameter: p}
7373
}
7474

7575
// Check if the parameter is of the expected type
7676
if _, ok := r.GetArguments()[p].(T); !ok {
77-
return zero, fmt.Errorf("parameter %s is not of type %T", p, zero)
77+
return zero, InvalidParameterTypeError{
78+
Parameter: p,
79+
Expected: fmt.Sprintf("%T", zero),
80+
Actual: fmt.Sprintf("%T", r.GetArguments()[p]),
81+
}
7882
}
7983

8084
if r.GetArguments()[p].(T) == zero {
81-
return zero, fmt.Errorf("missing required parameter: %s", p)
82-
85+
return zero, MissingRequiredParameterError{Parameter: p}
8386
}
8487

8588
return r.GetArguments()[p].(T), nil
@@ -112,7 +115,11 @@ func OptionalParam[T any](r mcp.CallToolRequest, p string) (T, error) {
112115

113116
// Check if the parameter is of the expected type
114117
if _, ok := r.GetArguments()[p].(T); !ok {
115-
return zero, fmt.Errorf("parameter %s is not of type %T, is %T", p, zero, r.GetArguments()[p])
118+
return zero, InvalidParameterTypeError{
119+
Parameter: p,
120+
Expected: fmt.Sprintf("%T", zero),
121+
Actual: fmt.Sprintf("%T", r.GetArguments()[p]),
122+
}
116123
}
117124

118125
return r.GetArguments()[p].(T), nil
@@ -163,13 +170,21 @@ func OptionalStringArrayParam(r mcp.CallToolRequest, p string) ([]string, error)
163170
for i, v := range v {
164171
s, ok := v.(string)
165172
if !ok {
166-
return []string{}, fmt.Errorf("parameter %s is not of type string, is %T", p, v)
173+
return []string{}, InvalidParameterTypeError{
174+
Parameter: p,
175+
Expected: "string",
176+
Actual: fmt.Sprintf("%T", v),
177+
}
167178
}
168179
strSlice[i] = s
169180
}
170181
return strSlice, nil
171182
default:
172-
return []string{}, fmt.Errorf("parameter %s could not be coerced to []string, is %T", p, r.GetArguments()[p])
183+
return []string{}, InvalidParameterTypeError{
184+
Parameter: p,
185+
Expected: "[]string",
186+
Actual: fmt.Sprintf("%T", r.GetArguments()[p]),
187+
}
173188
}
174189
}
175190

0 commit comments

Comments
 (0)