diff --git a/services/api/internal/search/meili.go b/services/api/internal/search/meili.go index 8666cb3..58993c3 100644 --- a/services/api/internal/search/meili.go +++ b/services/api/internal/search/meili.go @@ -77,6 +77,7 @@ func (m *Meili) wait(ctx context.Context, t taskRef) error { Status string `json:"status"` Error *struct { Message string `json:"message"` + Code string `json:"code"` } `json:"error"` } if err := m.do(ctx, http.MethodGet, fmt.Sprintf("/tasks/%d", t.TaskUID), nil, &task); err != nil { @@ -88,6 +89,10 @@ func (m *Meili) wait(ctx context.Context, t taskRef) error { case "failed", "canceled": msg := task.Status if task.Error != nil { + // Удаление отсутствующего индекса принимается (202) и падает уже в задаче. + if task.Error.Code == "index_not_found" { + return fmt.Errorf("%w: task %d: %s", ErrMeiliNotFound, t.TaskUID, task.Error.Message) + } msg = task.Error.Message } return fmt.Errorf("meilisearch: task %d: %s", t.TaskUID, msg) diff --git a/services/api/internal/search/meili_test.go b/services/api/internal/search/meili_test.go new file mode 100644 index 0000000..61b0a4e --- /dev/null +++ b/services/api/internal/search/meili_test.go @@ -0,0 +1,28 @@ +package search + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" +) + +// На чистом Meilisearch удаление несуществующего индекса падает в задаче, а не ответом 404. +func TestCallMapsFailedIndexTaskToNotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusAccepted) + _, _ = w.Write([]byte(`{"taskUid":0}`)) + return + } + _, _ = w.Write([]byte(`{"status":"failed","error":{"code":"index_not_found","message":"Index ` + "`x`" + ` not found."}}`)) + })) + defer srv.Close() + + err := NewMeili(srv.URL, "").call(context.Background(), http.MethodDelete, "/indexes/x", nil) + if !errors.Is(err, ErrMeiliNotFound) { + t.Fatalf("err = %v, want ErrMeiliNotFound", err) + } +}