{"record":{"id":"705562a3c700daae","repo":"bytebase/bytebase","slug":"failed-to-list-indices-unexpected-status-code-d","errorCode":null,"errorMessage":"failed to list indices: unexpected status code %d: %s","messagePattern":"failed to list indices: unexpected status code (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/elasticsearch/sync.go","lineNumber":304,"sourceCode":"\t\treturn indicesMetadata, nil\n\t}\n\n\t// Fallback to basic auth client\n\tresp, err := d.basicAuthClient.Do(\"GET\", []byte(\"/_cat/indices?format=json&pretty\"), nil)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"failed to list indices\")\n\t}\n\tdefer resp.Body.Close()\n\n\tbytes, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to read indices response body\")\n\t}\n\n\t// Check HTTP status code\n\tif resp.StatusCode != http.StatusOK {\n\t\t// Include response body for debugging\n\t\treturn nil, errors.Errorf(\"failed to list indices: unexpected status code %d: %s\", resp.StatusCode, string(bytes))\n\t}\n\n\tvar results []IndicesResult\n\tif err := json.Unmarshal(bytes, &results); err != nil {\n\t\t// Include response body to help debug parsing issues\n\t\tbodyPreview, truncated := common.TruncateString(string(bytes), 500)\n\t\tif truncated {\n\t\t\tbodyPreview += \"...\"\n\t\t}\n\t\treturn nil, errors.Wrapf(err, \"failed to parse indices response: %s\", bodyPreview)\n\t}\n\n\tfor _, m := range results {\n\t\tif hiddenIndices[m.Index] {\n\t\t\tcontinue\n\t\t}\n\n\t\tdatasize, err := unitConversion(m.IndexSize)","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/elasticsearch/sync.go#L286-L322","documentation":"In the basic-auth fallback of getIndices (sync.go:304), the _cat/indices response returned a non-200 status; the error includes the status code and the response body (e.g. an ES error JSON or auth challenge). Unlike the other wraps this is an errors.Errorf, not a wrap — the HTTP layer worked, the server said no.","triggerScenarios":"basicAuthClient.Do returned a response with StatusCode != 200 — 401/403 wrong credentials, 404 wrong path/base-URL, 503 cluster unavailable, or a security plugin redirecting to a login page.","commonSituations":"Username/password wrong or rotated; user lacking privileges for _cat/indices; base URL path prefix wrong (e.g. behind a path-rewriting reverse proxy); Elasticsearch Security enabled but the driver configured without credentials.","solutions":["Read the body embedded in the error — it states whether it's auth (401/403), not-found (404), or unavailable (503).","Fix credentials/permissions: ensure the user has monitor cluster + view index metadata privileges.","Verify the base URL and any reverse-proxy path rewriting preserve /_cat/indices.","If ES security is on, prefer the typed client with an API key over raw basic auth."],"exampleFix":"// before\nresp, err := d.basicAuthClient.Do(\"GET\", []byte(\"/_cat/indices?format=json&pretty\"), nil)\n// after — check status before reading body semantics\nresp, err := d.basicAuthClient.Do(\"GET\", []byte(\"/_cat/indices?format=json\"), nil)\nif err != nil {\n\treturn nil, errors.Wrapf(err, \"failed to list indices\")\n}\nif resp.StatusCode == http.StatusUnauthorized {\n\treturn nil, errors.New(\"elasticsearch auth failed: check username/password\")\n}","handlingStrategy":"validation","validationCode":"user, pass := instance.Credential\nreq, _ := http.NewRequest(\"GET\", instanceURL+\"/_cat/indices?format=json\", nil)\nreq.SetBasicAuth(user, pass)\nresp, err := http.DefaultClient.Do(req)\nif err != nil {\n\treturn err\n}\nif resp.StatusCode == 401 || resp.StatusCode == 403 {\n\treturn fmt.Errorf(\"elasticsearch credentials/permissions invalid (HTTP %d)\", resp.StatusCode)\n}","typeGuard":null,"tryCatchPattern":"if resp.StatusCode != http.StatusOK {\n\tbody, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))\n\tswitch resp.StatusCode {\n\tcase 401, 403:\n\t\treturn fmt.Errorf(\"auth failed listing indices: %s\", body)\n\tdefault:\n\t\treturn fmt.Errorf(\"unexpected status %d: %s\", resp.StatusCode, body)\n\t}\n}","preventionTips":["Validate credentials with a health/auth check when saving the instance","Grant the user monitor cluster + index metadata privileges","Watch for reverse proxies rewriting paths or returning their own error pages"],"tags":["elasticsearch","http","authentication"],"backgroundTag":"http-error-response","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}