{"record":{"id":"2175dad2308587fb","repo":"elastic/elasticsearch","slug":"uploading-snyk-graph-failed-with-http-code","errorCode":null,"errorMessage":"Uploading Snyk Graph failed with http code {}: {}","messagePattern":"Uploading Snyk Graph failed with http code (.+?): (.+?)","errorType":"http","errorClass":"GradleException","httpStatus":null,"severity":"error","filePath":"build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/snyk/UploadSnykDependenciesGraph.java","lineNumber":72,"sourceCode":"        token = objectFactory.property(String.class);\n        inputFile = objectFactory.fileProperty();\n    }\n\n    @TaskAction\n    void upload() {\n        String endpoint = calculateEffectiveEndpoint();\n        CloseableHttpResponse response;\n        try (CloseableHttpClient client = HttpClients.createDefault()) {\n            HttpPut putRequest = new HttpPut(endpoint);\n            putRequest.addHeader(\"Authorization\", \"token \" + token.get());\n            putRequest.addHeader(\"Content-Type\", \"application/json\");\n            putRequest.setEntity(new FileEntity(inputFile.getAsFile().get(), ContentType.APPLICATION_JSON));\n            response = client.execute(putRequest);\n            int statusCode = response.getCode();\n            String responseString = EntityUtils.toString(response.getEntity());\n            getLogger().info(\"Snyk API call response status: \" + statusCode);\n            if (statusCode != HttpURLConnection.HTTP_CREATED) {\n                throw new GradleException(\"Uploading Snyk Graph failed with http code \" + statusCode + \": \" + responseString);\n            }\n            getLogger().info(responseString);\n        } catch (IOException | ParseException e) {\n            throw new GradleException(\"Failed to call API endpoint to submit updated dependency graph\", e);\n        }\n    }\n\n    private String calculateEffectiveEndpoint() {\n        String url = this.url.get();\n        return snykOrganisation.map(id -> url + \"?org=\" + id).getOrElse(url);\n    }\n\n    @Input\n    public Property<String> getToken() {\n        return token;\n    }\n\n    @Input","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/snyk/UploadSnykDependenciesGraph.java#L54-L90","documentation":"GradleException thrown when the Snyk dependency-graph HTTP PUT returns any status other than HTTP 201 Created. The message includes the status code and the raw response body so the API's error text is surfaced.","triggerScenarios":"client.execute(putRequest) returns a non-201 code: 401/403 for a bad or expired token, 404 for a wrong endpoint URL, 422 for a malformed payload, 429 for rate limiting, or 5xx for a Snyk-side outage.","commonSituations":"SNYK_TOKEN expired or revoked; organisation id omitted or wrong causing the ?org= query to resolve to a forbidden org; payload JSON schema drifted from Snyk API expectations; network proxy rewriting the response.","solutions":["Read the response body in the message to see Snyk's specific error reason first.","Confirm the token is valid: rotate/re-check the Authorization header value for token.get().","Validate calculateEffectiveEndpoint() produces the correct Snyk API URL including the right org id.","If 429/5xx, retry with backoff or re-run the upload task later."],"exampleFix":"// before\nif (statusCode != HttpURLConnection.HTTP_CREATED) {\n    throw new GradleException(\"Uploading Snyk Graph failed with http code \" + statusCode + \": \" + responseString);\n}\n\n// after - tolerate 200/201 and surface Snyk request id\nif (statusCode != HttpURLConnection.HTTP_CREATED && statusCode != HttpURLConnection.HTTP_OK) {\n    throw new GradleException(\"Uploading Snyk Graph failed with http code \" + statusCode + \": \" + responseString);\n}","handlingStrategy":"try-catch","validationCode":"// Validate the token and endpoint shape before uploading\nif (token.get() == null || token.get().isBlank()) {\n    throw new GradleException(\"Snyk token not set; skipping upload\");\n}\nString endpoint = calculateEffectiveEndpoint();\nif (!endpoint.startsWith(\"https://\")) {\n    throw new GradleException(\"Snyk endpoint must be https: \" + endpoint);\n}","typeGuard":null,"tryCatchPattern":"try {\n    // upload logic\n} catch (GradleException e) {\n    if (e.getMessage().contains(\"failed with http code\")) {\n        getLogger().warn(\"Snyk upload rejected: {}\", e.getMessage());\n        // surface but do not fail the build if upload is best-effort\n        return;\n    }\n    throw e;\n}","preventionTips":["Rotate and verify the Snyk token before running the upload task.","Confirm the org id and endpoint URL in the task configuration.","Treat the upload as best-effort (continue-on-error) if Snyk availability is not release-critical."],"tags":["snyk","http","api-upload","build-tooling","network"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}