diff --git a/handlers/items.go b/handlers/items.go index 16c0f49..7cb90ea 100644 --- a/handlers/items.go +++ b/handlers/items.go @@ -24,14 +24,19 @@ func GetItems(w http.ResponseWriter, r *http.Request) { storeMu.RLock() defer storeMu.RUnlock() - // BUG: when store is nil (fresh start), json.Marshal(nil map values) - // causes unexpected behavior — should return [] but may panic or 500. + w.Header().Set("Content-Type", "application/json") + + if store == nil || len(store) == 0 { + w.WriteHeader(http.StatusOK) + w.Write([]byte("[]")) + return + } + items := make([]Item, 0, len(store)) for _, item := range store { items = append(items, item) } - w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(items); err != nil { http.Error(w, `{"error":"internal server error"}`, http.StatusInternalServerError) }