{"record":{"id":"2baeda78e08ee3fc","repo":"googleapis/mcp-toolbox","slug":"failed-to-unmarshal-json-v","errorCode":null,"errorMessage":"failed to unmarshal json: %v","messagePattern":"failed to unmarshal json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/dgraph/dgraph.go","lineNumber":370,"sourceCode":"\tresp, err := hc.httpClient.Do(req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error performing request: %w\", err)\n\t}\n\n\tdefer resp.Body.Close()\n\tdata, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn err\n\t}\n\tvar result []struct {\n\t\tInstance string `json:\"instance\"`\n\t\tAddress  string `json:\"address\"`\n\t\tStatus   string `json:\"status\"`\n\t}\n\n\t// Unmarshal response into the struct\n\tif err := json.Unmarshal(data, &result); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal json: %v\", err)\n\t}\n\n\tif len(result) == 0 {\n\t\treturn fmt.Errorf(\"health info should not empty for: %v\", url)\n\t}\n\n\tvar unhealthyErr error\n\tfor _, info := range result {\n\t\tif info.Status != \"healthy\" {\n\t\t\tunhealthyErr = fmt.Errorf(\"dgraph instance [%v] is not in healthy state, address is %v\",\n\t\t\t\tinfo.Instance, info.Address)\n\t\t} else {\n\t\t\treturn nil\n\t\t}\n\t}\n\n\treturn unhealthyErr\n}","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/dgraph/dgraph.go#L352-L388","documentation":"Thrown by the Dgraph source's healthCheck when the /health response body cannot be unmarshaled into the expected JSON array of health entries (instance, address, status). Dgraph returned a 200/body that is not the expected health JSON — often an HTML error page, a proxy message, or an empty body. Wrapped with %v.","triggerScenarios":"healthCheck reads the /health response with io.ReadAll and json.Unmarshal(data, &result) fails because the body is not a JSON array of objects with instance/address/status fields (e.g. HTML from a reverse proxy, or a JSON object instead of an array).","commonSituations":"A load balancer or ingress returns an HTML 502/404 page; pointing baseUrl at the Dgraph HTTP UI or a different service that returns different JSON; a Dgraph version changing the health payload shape.","solutions":["curl the exact /health URL and confirm the body is a JSON array like [{\"instance\":\"...\",\"address\":\"...\",\"status\":\"healthy\"}].","Ensure baseUrl points at the Dgraph Alpha/Zero HTTP port, not a proxy, UI, or gRPC port.","Check whether an intermediary (nginx/ingress) is intercepting the request and returning non-JSON responses.","Verify the Dgraph version still serves the same /health schema."],"exampleFix":"// before: pointing at the UI/proxy\nbaseUrl: http://dgraph-ui:8080\n// after: point at the Alpha HTTP endpoint\nbaseUrl: http://dgraph-alpha:8080","handlingStrategy":"validation","validationCode":"resp, err := http.Get(baseURL + \"/health\")\nif err != nil { return err }\nbody, _ := io.ReadAll(resp.Body)\nvar probe []map[string]interface{}\nif err := json.Unmarshal(body, &probe); err != nil {\n    return fmt.Errorf(\"/health returned non-JSON body (first 100 bytes: %q) — check for proxy/UI at this address\", string(body[:min(100, len(body))]))\n}","typeGuard":null,"tryCatchPattern":"if err := healthCheck(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to unmarshal json\") {\n        // non-JSON body: capture the raw response for diagnosis\n        log.Printf(\"dgraph /health returned unexpected body; raw: %s\", rawBody)\n        return err\n    }\n    return err\n}","preventionTips":["Point baseUrl directly at the Dgraph Alpha/Zero HTTP endpoint, never through a UI or HTML-serving proxy.","curl the /health endpoint once and confirm the body is a JSON array before configuring.","Pin and test the Dgraph version; verify the health payload shape after upgrades."],"tags":["dgraph","json","health-check","unmarshal"],"backgroundTag":"json-unmarshal-failed","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"}