{"record":{"id":"86c143664637a8d4","repo":"googleapis/mcp-toolbox","slug":"error-parsing-json-v","errorCode":null,"errorMessage":"error parsing JSON: %v","messagePattern":"error parsing JSON: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/dgraph/dgraph.go","lineNumber":138,"sourceCode":"}\n\nfunc (s *Source) RunSQL(statement string, params parameters.ParamValues, isQuery bool, timeout string) (any, error) {\n\tparamsMap := params.AsMapWithDollarPrefix()\n\tresp, err := s.DgraphClient().ExecuteQuery(statement, paramsMap, isQuery, timeout)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif err := checkError(resp); err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar result struct {\n\t\tData map[string]interface{} `json:\"data\"`\n\t}\n\n\tif err := json.Unmarshal(resp, &result); err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing JSON: %v\", err)\n\t}\n\n\treturn result.Data, nil\n}\n\nfunc initDgraphHttpClient(ctx context.Context, tracer trace.Tracer, r Config) (*DgraphClient, error) {\n\t//nolint:all // Reassigned ctx\n\tctx, span := sources.InitConnectionSpan(ctx, tracer, SourceType, r.Name)\n\tdefer span.End()\n\n\tif r.DgraphUrl == \"\" {\n\t\treturn nil, fmt.Errorf(\"dgraph url should not be empty\")\n\t}\n\n\thc := &DgraphClient{\n\t\thttpClient: &http.Client{},\n\t\tbaseUrl:    r.DgraphUrl,\n\t\tHttpToken: &HttpToken{","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/dgraph/dgraph.go#L120-L156","documentation":"RunSQL posts a DQL query to the Dgraph /query endpoint and unmarshals the response body into a struct with a Data field. This error means the HTTP response body was not valid JSON (or not the expected shape). Dgraph typically returns JSON, so this usually indicates an error page, empty body, or a non-JSON response (proxy/auth layer).","triggerScenarios":"Calling RunSQL when the Dgraph server (or an intermediary) returns a non-JSON body: empty response, HTML error page from a reverse proxy, plain-text auth failure, or truncated response.","commonSituations":"Wrong DgraphUrl pointing at a non-Dgraph service, an ingress/load balancer returning 502 HTML, TLS termination issues returning an error string, Dgraph returning gzipped body handled incorrectly.","solutions":["Log the raw response body and HTTP status code before unmarshaling to see what was actually returned","Verify DgraphUrl points to the correct Dgraph HTTP port (default 8080), not gRPC (9080)","Check for proxies/ingress returning non-JSON error pages","Return the raw body in the error message to aid debugging"],"exampleFix":"// before\nif err := json.Unmarshal(resp, &result); err != nil {\n\treturn nil, fmt.Errorf(\"error parsing JSON: %v\", err)\n}\n// after\nif err := json.Unmarshal(resp, &result); err != nil {\n\treturn nil, fmt.Errorf(\"error parsing JSON: %v (body: %s)\", err, string(resp))\n}","handlingStrategy":"validation","validationCode":"u, err := url.Parse(dgraphUrl)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n\treturn fmt.Errorf(\"dgraphUrl must be a valid http(s) URL\")\n}\n// health-check before querying:\n// resp, err := http.Get(dgraphUrl + \"/health\")","typeGuard":null,"tryCatchPattern":"data, err := svc.RunSQL(ctx, query)\nif err != nil && strings.Contains(err.Error(), \"error parsing JSON\") {\n\t// inspect raw response: wrong endpoint, proxy error page, or auth failure\n}","preventionTips":["Point dgraphUrl at the Dgraph HTTP port (8080), not gRPC (9080)","Include the raw response body in error logs for diagnosis","Health-check the Dgraph endpoint during startup"],"tags":["dgraph","json","http","parsing"],"backgroundTag":"invalid-json-response","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}