{"record":{"id":"f1a23ee303c861b0","repo":"apache/cassandra","slug":"httpexception-conn-getresponsecode-conn-getresp","errorCode":null,"errorMessage":"HttpException(conn.getResponseCode(), conn.getResponseMessage())","messagePattern":"HttpException\\(conn\\.getResponseCode\\(\\), conn\\.getResponseMessage\\(\\)\\)","errorType":"http","errorClass":"HttpException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java","lineNumber":115,"sourceCode":"        return apiCall(metadataServiceUrl, query, \"GET\", extraHeaders, 200);\n    }\n\n    public String apiCall(String url,\n                          String query,\n                          String method,\n                          Map<String, String> extraHeaders,\n                          int expectedResponseCode) throws IOException\n    {\n        HttpURLConnection conn = null;\n        try\n        {\n            // Populate the region and zone by introspection, fail if 404 on metadata\n            conn = (HttpURLConnection) new URL(url + query).openConnection();\n            extraHeaders.forEach(conn::setRequestProperty);\n            conn.setRequestMethod(method);\n            conn.setConnectTimeout(requestTimeoutMs);\n            if (conn.getResponseCode() != expectedResponseCode)\n                throw new HttpException(conn.getResponseCode(), conn.getResponseMessage());\n\n            // Read the information. I wish I could say (String) conn.getContent() here...\n            int cl = conn.getContentLength();\n\n            if (cl == -1)\n                return null;\n\n            byte[] b = new byte[cl];\n            try (DataInputStream d = new DataInputStream((InputStream) conn.getContent()))\n            {\n                d.readFully(b);\n            }\n            return new String(b, UTF_8);\n        }\n        finally\n        {\n            if (conn != null)\n                conn.disconnect();","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java#L97-L133","documentation":"In apiCall, the connector performs an HTTP request to the cloud metadata service (EC2/GCE metadata endpoint). If the HTTP response code does not match the expected one (typically 200), it throws HttpException carrying the actual response code and message. This signals that the metadata service responded but not with the anticipated status.","triggerScenarios":"Calling apiCall (directly or via lookups of region/zone/instance-id) when the metadata URL returns a non-expected status — e.g. 404 for an unsupported metadata path, 401/403 for protected metadata endpoints (IMDSv2 token required), or 5xx from an overloaded metadata service.","commonSituations":"EC2 instance metadata v2 (IMDSv2) requiring a token while the connector does a plain GET; wrong metadata endpoint version in the URL; metadata service disabled on the instance; proxy/firewall rewriting responses; query path changed by the cloud provider.","solutions":["Inspect the response code in the exception to identify 404 vs 4xx/5xx and align the metadata URL/path","For AWS, allow token-based IMDSv2 or set instance metadata options to 'optional'","Verify the instance can reach 169.254.169.254 (or the configured endpoint) with curl","Check that requestTimeoutMs is not causing truncated responses; increase cassandra.metadata_request_timeout","If the endpoint intentionally misses (e.g. zone lookup), handle the null/404 path rather than failing"],"exampleFix":"// before (assuming plain GET works)\nString zone = connector.apiCall(\"/latest/meta-data/placement/availability-zone\", -1);\n// after (guard against unexpected status)\ntry {\n    String zone = connector.apiCall(\"/latest/meta-data/placement/availability-zone\", -1);\n} catch (HttpException e) {\n    logger.warn(\"Metadata service returned {} {}\", e.code, e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"// Probe the metadata endpoint before relying on it\ncurl -s -o /dev/null -w \"%{http_code}\" http://169.254.169.254/latest/meta-data/ || echo unreachable","typeGuard":null,"tryCatchPattern":"try {\n    String value = connector.apiCall(path, HttpURLConnection.HTTP_OK);\n} catch (HttpException e) {\n    logger.warn(\"Metadata endpoint returned HTTP {} for {}\", e.getResponseCode(), path);\n    // fall back to configured defaults\n}","preventionTips":["Verify instance metadata service reachability when provisioning nodes","Keep metadata endpoint paths matching the cloud provider's current API version","Configure IMDSv2 token requirements to allow your access pattern","Monitor for metadata service 5xx and set a sane request timeout"],"tags":["http","cloud-metadata","network","snitch"],"backgroundTag":"http-error-response","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}