{"record":{"id":"ba469e38d1451f20","repo":"hashicorp/nomad","slug":"failed-querying-self-endpoint-s","errorCode":null,"errorMessage":"failed querying self endpoint: %s","messagePattern":"failed querying self endpoint: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/agent.go","lineNumber":59,"sourceCode":"\t// Prune indicates whether to remove a node from the list of members\n\tPrune bool\n}\n\n// Agent returns a new agent which can be used to query\n// the agent-specific endpoints.\nfunc (c *Client) Agent() *Agent {\n\treturn &Agent{client: c}\n}\n\n// Self is used to query the /v1/agent/self endpoint and\n// returns information specific to the running agent.\nfunc (a *Agent) Self() (*AgentSelf, error) {\n\tvar out *AgentSelf\n\n\t// Query the self endpoint on the agent\n\t_, err := a.client.query(\"/v1/agent/self\", &out, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed querying self endpoint: %s\", err)\n\t}\n\n\t// Populate the cache for faster queries\n\ta.populateCache(out)\n\n\treturn out, nil\n}\n\n// populateCache is used to insert various pieces of static\n// data into the agent handle. This is used during subsequent\n// lookups for the same data later on to save the round trip.\nfunc (a *Agent) populateCache(self *AgentSelf) {\n\tif a.nodeName == \"\" {\n\t\ta.nodeName = self.Member.Name\n\t}\n\tif a.datacenter == \"\" {\n\t\tif val, ok := self.Config[\"Datacenter\"]; ok {\n\t\t\ta.datacenter, _ = val.(string)","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/api/agent.go#L41-L77","documentation":"api/agent.go's Agent.Self() queries the local agent's /v1/agent/self endpoint; any error from the HTTP query is wrapped as \"failed querying self endpoint: %s\". This typically means the agent is unreachable, not running, or returned a non-2xx response.","triggerScenarios":"Calling Agent.Self() (directly or via NodeName(), Datacenter(), Region(), which call Self) when the Consul agent HTTP API is unreachable or returns an error status.","commonSituations":"Agent not running or wrong address/port in the Consul client config; ACL token lacking agent read permissions (403); firewall/network issues; container service discovery pointing at the wrong host.","solutions":["Verify the Consul agent is running and the client address/port are correct (e.g. 127.0.0.1:8500).","Check the wrapped error: 403 means set a valid ACL token via config ACLToken; connection refused means fix networking.","Test the endpoint manually with `curl http://<agent>:8500/v1/agent/self`.","If relying on NodeName/Datacenter/Region, ensure agent connectivity before caching."],"exampleFix":"// before\nclient, _ := api.NewClient(api.DefaultConfig())\nname, err := client.Agent().NodeName() // fails: wrong port\n// after\ncfg := api.DefaultConfig()\ncfg.Address = \"127.0.0.1:8500\"\ncfg.Token = os.Getenv(\"CONSUL_HTTP_TOKEN\")\nclient, _ := api.NewClient(cfg)\nname, err := client.Agent().NodeName()","handlingStrategy":"try-catch","validationCode":"// probe the agent before calling Self\ndialTimeout := 2 * time.Second\nconn, err := net.DialTimeout(\"tcp\", \"127.0.0.1:8500\", dialTimeout)\nif err != nil {\n    return fmt.Errorf(\"consul agent not reachable: %w\", err)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"self, err := client.Agent().Self()\nif err != nil {\n    if strings.Contains(err.Error(), \"failed querying self endpoint\") {\n        // check agent health / ACL token, optionally retry once\n        return retryOrFallback()\n    }\n    return err\n}","preventionTips":["Verify CONSUL_HTTP_ADDR / client.Address points at a live agent before starting.","Provide an ACL token with `agent:read` when ACLs are enabled.","Add readiness checks that hit /v1/agent/self before relying on NodeName/Region."],"tags":["http","api","network","consul-agent"],"backgroundTag":"http-request-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}