Skip to content

Commit 683641d

Browse files
committed
Return error type for unknown tools in inventory builder and handle it in HTTP handler
1 parent a209b8e commit 683641d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/http/handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import (
44
"context"
5+
"errors"
56
"log/slog"
67
"net/http"
78

@@ -178,6 +179,12 @@ func withInsiders(next http.Handler) http.Handler {
178179
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
179180
inv, err := h.inventoryFactoryFunc(r)
180181
if err != nil {
182+
if errors.Is(err, inventory.UnknownToolsErr) {
183+
w.WriteHeader(http.StatusBadRequest)
184+
w.Write([]byte(err.Error()))
185+
return
186+
}
187+
181188
w.WriteHeader(http.StatusInternalServerError)
182189
return
183190
}

pkg/inventory/builder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package inventory
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"sort"
78
"strings"
89
)
910

11+
var (
12+
UnknownToolsErr = errors.New("unknown tools specified in WithTools")
13+
)
14+
1015
// ToolFilter is a function that determines if a tool should be included.
1116
// Returns true if the tool should be included, false to exclude it.
1217
type ToolFilter func(ctx context.Context, tool *ServerTool) (bool, error)
@@ -204,7 +209,7 @@ func (b *Builder) Build() (*Inventory, error) {
204209

205210
// Error out if there are unrecognized tools
206211
if len(unrecognizedTools) > 0 {
207-
return nil, fmt.Errorf("unrecognized tools: %s", strings.Join(unrecognizedTools, ", "))
212+
return nil, fmt.Errorf("%w: %s", UnknownToolsErr, strings.Join(unrecognizedTools, ", "))
208213
}
209214
}
210215

0 commit comments

Comments
 (0)