فهرست منبع

fix(googlereader): return 400 when disable-tag would remove every category

Leaving a user without any category is a client error, not a server
error. The store now returns ErrLastCategory so the handler can tell it
apart from database failures.
Frédéric Guillot 9 ساعت پیش
والد
کامیت
2354ef1c9c
3فایلهای تغییر یافته به همراه10 افزوده شده و 5 حذف شده
  1. 4 0
      internal/googlereader/handler.go
  2. 1 3
      internal/googlereader/tag_integration_test.go
  3. 5 2
      internal/storage/category.go

+ 4 - 0
internal/googlereader/handler.go

@@ -780,6 +780,10 @@ func (h *greaderHandler) disableTagHandler(w http.ResponseWriter, r *http.Reques
 	}
 	}
 
 
 	err = h.store.RemoveAndReplaceCategoriesByName(userID, titles)
 	err = h.store.RemoveAndReplaceCategoriesByName(userID, titles)
+	if errors.Is(err, storage.ErrLastCategory) {
+		response.JSONBadRequest(w, r, err)
+		return
+	}
 	if err != nil {
 	if err != nil {
 		response.JSONServerError(w, r, err)
 		response.JSONServerError(w, r, err)
 		return
 		return

+ 1 - 3
internal/googlereader/tag_integration_test.go

@@ -146,9 +146,7 @@ func TestDisableTagRejectsInvalidRequests(t *testing.T) {
 		{"invalid stream", []string{"invalid"}, http.StatusBadRequest},
 		{"invalid stream", []string{"invalid"}, http.StatusBadRequest},
 		{"not a label", []string{feedStream(1)}, http.StatusBadRequest},
 		{"not a label", []string{feedStream(1)}, http.StatusBadRequest},
 		{"a label and a state", []string{labelStream("All"), starredStream}, http.StatusBadRequest},
 		{"a label and a state", []string{labelStream("All"), starredStream}, http.StatusBadRequest},
-
-		// The store refuses to remove the last category, and the handler reports it as a server error.
-		{"last category", []string{labelStream("All")}, http.StatusInternalServerError},
+		{"last category", []string{labelStream("All")}, http.StatusBadRequest},
 	}
 	}
 
 
 	for _, scenario := range scenarios {
 	for _, scenario := range scenarios {

+ 5 - 2
internal/storage/category.go

@@ -15,6 +15,9 @@ import (
 // ErrNoCategory is returned when a user does not have any category.
 // ErrNoCategory is returned when a user does not have any category.
 var ErrNoCategory = errors.New("store: no category found")
 var ErrNoCategory = errors.New("store: no category found")
 
 
+// ErrLastCategory is returned when removing categories would leave a user without any category.
+var ErrLastCategory = errors.New("store: at least 1 category must remain after deletion")
+
 // AnotherCategoryExists checks if another category exists with the same title.
 // AnotherCategoryExists checks if another category exists with the same title.
 func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
 func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
 	var result bool
 	var result bool
@@ -252,7 +255,7 @@ func (s *Storage) RemoveCategory(userID, categoryID int64) error {
 
 
 // RemoveAndReplaceCategoriesByName deletes categories with the given titles and
 // RemoveAndReplaceCategoriesByName deletes categories with the given titles and
 // reassigns affected feeds to the user's first remaining category. It returns
 // reassigns affected feeds to the user's first remaining category. It returns
-// an error if the deletion would leave the user without any categories.
+// ErrLastCategory if the deletion would leave the user without any categories.
 func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string) error {
 func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string) error {
 	tx, err := s.db.Begin()
 	tx, err := s.db.Begin()
 	if err != nil {
 	if err != nil {
@@ -268,7 +271,7 @@ func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string
 		return fmt.Errorf("store: unable to retrieve category count: %v", err)
 		return fmt.Errorf("store: unable to retrieve category count: %v", err)
 	}
 	}
 	if count < 1 {
 	if count < 1 {
-		return errors.New("store: at least 1 category must remain after deletion")
+		return ErrLastCategory
 	}
 	}
 
 
 	query = `
 	query = `