Skip to content

Commit

Permalink
fix(index): correcting the not found errors when no nodes were present (
Browse files Browse the repository at this point in the history
#6)

correcting the not found errors when no nodes were present
  • Loading branch information
Jacobbrewer1 authored Dec 19, 2023
1 parent e6a73a2 commit 73364e0
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions cmd/summary/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
var env entities.Environment
if !ok {
env = entities.EnvAll
} else if envStr == "" {
// Respond with 400 bad request.
w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(request.NewMessage("No environment provided")); err != nil {
slog.Error("Error encoding response", slog.String(logging.KeyError, err.Error()))
}
return
} else {
envStr = strings.ToUpper(envStr)
env = entities.Environment(envStr)
Expand All @@ -44,16 +37,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
}

nodes, err := dataaccess.DB.GetRuns(r.Context())
if err != nil {
if errors.Is(err, dataaccess.ErrNotFound) {
// Respond with 404 not found.
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(request.NewMessage("No nodes found")); err != nil {
slog.Error("Error encoding response", slog.String(logging.KeyError, err.Error()))
}
return
}

if err != nil && !errors.Is(err, dataaccess.ErrNotFound) {
// Respond with 500 internal server error.
w.WriteHeader(http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode(request.NewMessage("Error getting nodes")); err != nil {
Expand All @@ -62,15 +46,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
return
}

if len(nodes) == 0 {
// Respond with 404 not found.
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(request.NewMessage("No nodes found")); err != nil {
slog.Error("Error encoding response", slog.String(logging.KeyError, err.Error()))
}
return
}

// Filter the nodes by environment.
filteredNodes := make([]*entities.PuppetRun, 0, len(nodes))
for _, node := range nodes {
Expand Down

0 comments on commit 73364e0

Please sign in to comment.