{"record":{"id":"3ca345bb3ed25430","repo":"bytebase/bytebase","slug":"failed-to-create-elasticsearch-client","errorCode":null,"errorMessage":"failed to create elasticsearch client","messagePattern":"failed to create elasticsearch client","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/elasticsearch/elasticsearch.go","lineNumber":169,"sourceCode":"\t\t\tInsecureSkipVerify: true,\n\t\t}\n\t} else {\n\t\t// Ensure minimum TLS version\n\t\ttlsConfig.MinVersion = tls.VersionTLS12\n\t}\n\n\tesConfig := newElasticsearchConfig(config, address, tlsConfig)\n\t// default http client.\n\thttpClient := &http.Client{\n\t\tTransport: &http.Transport{\n\t\t\tTLSClientConfig: tlsConfig,\n\t\t},\n\t}\n\n\t// typed elasticsearch client.\n\ttypedClient, err := elasticsearch.NewClient(esConfig)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"failed to create elasticsearch client\")\n\t}\n\n\t// generate basic authentication string for http client.\n\tencodedUsrAndPasswd := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(\"%s:%s\", config.DataSource.Username, config.Password)))\n\tbasicAuthString := fmt.Sprintf(\"Basic %s\", string(encodedUsrAndPasswd))\n\n\treturn &Driver{\n\t\ttypedClient: typedClient,\n\t\tbasicAuthClient: &BasicAuthClient{\n\t\t\thttpClient: httpClient,\n\t\t\taddrScheduler: &AddressScheduler{\n\t\t\t\taddresses: []string{address},\n\t\t\t\tcount:     0,\n\t\t\t},\n\t\t\tbasicAuthString: basicAuthString,\n\t\t},\n\t\tconfig: config,\n\t}, nil","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/elasticsearch/elasticsearch.go#L151-L187","documentation":"elasticsearch.NewClient (go-elasticsearch typed client) returns an error only when the client configuration itself is invalid — it does not contact the server. Wrapped here as \"failed to create elasticsearch client\". Typical causes are a malformed address in esConfig.Addresses or a CA certificate the client cannot decode.","triggerScenarios":"Open with basic-auth auth type where the assembled address (e.g. \"http://:9200\" when Host is empty) or CACert bytes are rejected by the elasticsearch-go client constructor.","commonSituations":"Empty host field producing an address like \"http://:9200\"; CA cert blob that is not decodable PEM; incompatible go-elasticsearch config after a library version upgrade.","solutions":["Ensure the datasource Host is non-empty and forms a valid address; check the wrapped error for the exact URL complaint.","Validate the SslCa PEM content; clear it if the cluster uses a CA your OS already trusts.","Check the wrapped error from elasticsearch.NewClient for the specific config field it rejects and fix that field.","If this appeared after a dependency bump, review the go-elasticsearch changelog for Config validation changes."],"exampleFix":"// before\nif config.DataSource.Host == \"\" { /* falls through */ }\n// after\nif config.DataSource.Host == \"\" {\n\treturn nil, errors.New(\"elasticsearch host is required\")\n}","handlingStrategy":"validation","validationCode":"if ds.Host == \"\" {\n\treturn errors.New(\"elasticsearch host is required\")\n}\nif _, err := url.Parse(fmt.Sprintf(\"http://%s:%s\", ds.Host, ds.Port)); err != nil {\n\treturn fmt.Errorf(\"invalid address: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if _, err := elasticsearch.NewClient(cfg); err != nil {\n\tvar cfgErr *elasticsearch.Error\n\tif errors.As(err, &cfgErr) {\n\t\tlog.Printf(\"elasticsearch client config invalid: %v\", cfgErr)\n\t}\n\treturn fmt.Errorf(\"failed to create elasticsearch client: %w\", err)\n}","preventionTips":["Require a non-empty host before constructing the client config.","Only set CACert when the cluster actually uses a custom CA.","Pin and test the go-elasticsearch version; re-verify config fields after upgrades."],"tags":["elasticsearch","client-configuration","initialization"],"backgroundTag":"invalid-config-value","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"}