{"record":{"id":"7da5cdd434b9a66d","repo":"jaegertracing/jaeger","slug":"failed-to-check-if-template-exists-w","errorCode":null,"errorMessage":"failed to check if template exists: %w","messagePattern":"failed to check if template exists: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/storage/elasticsearch/esclient/index_client.go","lineNumber":369,"sourceCode":"\t\treturn \"_index_template/\" + name\n\t}\n\treturn \"_template/\" + name\n}\n\n// TestsOnlyTemplateExists reports whether the index template for name exists,\n// using the same endpoint CreateTemplate installs it under. Integration-test-only\n// — production never checks a template's existence.\nfunc (i IndicesClient) TestsOnlyTemplateExists(ctx context.Context, name string) (bool, error) {\n\t_, err := i.request(ctx, elasticRequest{\n\t\tendpoint: i.templateEndpoint(name),\n\t\tmethod:   http.MethodHead,\n\t})\n\tif err != nil {\n\t\tvar responseError ResponseError\n\t\tif errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {\n\t\t\treturn false, nil\n\t\t}\n\t\treturn false, fmt.Errorf(\"failed to check if template exists: %w\", err)\n\t}\n\treturn true, nil\n}\n\n// TestsOnlyDeleteTemplate deletes the index template for name (the same endpoint\n// CreateTemplate installs it under), tolerating a missing template. Integration-\n// test-only — production never deletes templates.\nfunc (i IndicesClient) TestsOnlyDeleteTemplate(ctx context.Context, name string) error {\n\t_, err := i.request(ctx, elasticRequest{\n\t\tendpoint: i.templateEndpoint(name),\n\t\tmethod:   http.MethodDelete,\n\t})\n\tif err != nil {\n\t\tvar responseError ResponseError\n\t\tif errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {\n\t\t\treturn nil\n\t\t}\n\t\treturn fmt.Errorf(\"failed to delete template %q: %w\", name, err)","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/jaegertracing/jaeger/blob/806f4447841ecdb60519f408b004a599d515f437/internal/storage/elasticsearch/esclient/index_client.go#L351-L387","documentation":"Returned by the test-only IndicesClient.TestsOnlyTemplateExists when the HEAD request against the template endpoint (_index_template/<name> on v8, _template/<name> otherwise) fails with anything other than a 404 ResponseError. 404 means the template is absent (false, nil); any other failure yields this wrapped error. It indicates the probe failed, not that the template is missing.","triggerScenarios":"HEAD on the template endpoint fails without a 404 ResponseError: ES unreachable in the integration-test environment, auth failure (401/403), timeout, or non-JSON error responses.","commonSituations":"Integration tests running against a not-yet-ready ES container; wrong ES version image where the v8 vs legacy endpoint routing mismatches; test credentials/permissions blocking HEAD on _index_template.","solutions":["Ensure the test ES container is healthy before running (wait for cluster health)","Verify the backend version detection so templateEndpoint targets the right API (_index_template vs _template)","Check test-user permissions for template APIs","Retry the check — it is read-only and safe to repeat"],"exampleFix":"// before\nexists, err := client.TestsOnlyTemplateExists(ctx, \"jaeger-span\")\nrequire.NoError(t, err)\n// after\nexists, err := client.TestsOnlyTemplateExists(ctx, \"jaeger-span\")\nif err != nil {\n    var respErr esclient.ResponseError\n    if errors.As(err, &respErr) {\n        t.Fatalf(\"template probe rejected, status=%d\", respErr.StatusCode)\n    }\n    t.Fatalf(\"ES unreachable during test: %v\", err)\n}","handlingStrategy":"retry","validationCode":"// test pre-flight: wait for ES readiness before template checks\nrequire.Eventually(t, func() bool {\n    resp, err := http.Get(esURL + \"/_cluster/health\")\n    return err == nil && resp.StatusCode == 200\n}, 30*time.Second, time.Second)","typeGuard":"var respErr esclient.ResponseError\nisMissing := func(err error) bool {\n    var re esclient.ResponseError\n    return errors.As(err, &re) && re.StatusCode == http.StatusNotFound\n}","tryCatchPattern":"exists, err := client.TestsOnlyTemplateExists(ctx, name)\nif err != nil {\n    var respErr esclient.ResponseError\n    if errors.As(err, &respErr) {\n        t.Fatalf(\"template probe rejected, status=%d\", respErr.StatusCode)\n    }\n    t.Fatalf(\"ES unreachable during template probe: %v\", err)\n}","preventionTips":["Wait for ES container health before running integration assertions","Confirm version-based endpoint routing (_index_template vs _template) matches the test image","Remember 404 is a valid 'not exists' answer; only non-404 failures are errors"],"tags":["elasticsearch","testing","index-template"],"backgroundTag":"elasticsearch-request-failed","analyzedSha":"806f4447841ecdb60519f408b004a599d515f437","analyzedAt":"2026-09-01T02:39:22.140Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}