Skip to content

Commit

Permalink
fix(search): return user error when disabled engines are received ins…
Browse files Browse the repository at this point in the history
…tead of ignoring
  • Loading branch information
aleksasiriski committed Nov 15, 2024
1 parent c4e50c5 commit 583a59d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
9 changes: 8 additions & 1 deletion src/router/routes/route_search_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ func routeSearchImages(w http.ResponseWriter, r *http.Request, ver string, disab
Value: fmt.Sprintf("%v", err),
})
}
catConf.DisableEngines(disabledEngines)

if catConf.ContainsDisabledEngines(disabledEngines) {
// User error.
return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{
Message: "category contains disabled engines",
Value: "disabled engines",
})
}

// All of these have default values set and validated.
opts := options.Options{
Expand Down
9 changes: 8 additions & 1 deletion src/router/routes/route_search_suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ func routeSearchSuggestions(w http.ResponseWriter, r *http.Request, ver string,
Value: fmt.Sprintf("%v", err),
})
}
catConf.DisableEngines(disabledEngines)

if catConf.ContainsDisabledEngines(disabledEngines) {
// User error.
return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{
Message: "category contains disabled engines",
Value: "disabled engines",
})
}

// All of these have default values set and validated.
opts := options.Options{
Expand Down
9 changes: 8 additions & 1 deletion src/router/routes/route_search_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ func routeSearchWeb(w http.ResponseWriter, r *http.Request, ver string, disabled
Value: fmt.Sprintf("%v", err),
})
}
catConf.DisableEngines(disabledEngines)

if catConf.ContainsDisabledEngines(disabledEngines) {
// User error.
return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{
Message: "category contains disabled engines",
Value: "disabled engines",
})
}

// All of these have default values set and validated.
opts := options.Options{
Expand Down
27 changes: 0 additions & 27 deletions src/search/category/disable.go

This file was deleted.

19 changes: 19 additions & 0 deletions src/search/category/disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package category

import (
"slices"

"github.com/hearchco/agent/src/search/engines"
)

// Returns true if the category contains any disabled engines.
// Otherwise, returns false.
func (c Category) ContainsDisabledEngines(disabledEngines []engines.Name) bool {
for _, eng := range disabledEngines {
if slices.Contains(c.Engines, eng) {
return true
}
}

return false
}

0 comments on commit 583a59d

Please sign in to comment.