{"record":{"id":"c00c5e856f1b892c","repo":"googleapis/mcp-toolbox","slug":"failed-to-marshal-credentials-v","errorCode":null,"errorMessage":"failed to marshal credentials: %v","messagePattern":"failed to marshal credentials: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/sources/dgraph/dgraph.go","lineNumber":295,"sourceCode":"\treturn hc.doLogin(credentials)\n}\n\nfunc (hc *DgraphClient) loginWithToken() error {\n\tcredentials := map[string]interface{}{\n\t\t\"refreshJWT\": hc.RefreshToken,\n\t\t\"namespace\":  hc.Namespace,\n\t}\n\treturn hc.doLogin(credentials)\n}\n\nfunc (hc *DgraphClient) doLogin(creds map[string]interface{}) error {\n\turl, err := getUrl(hc.baseUrl, \"/login\", nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tpayload, err := json.Marshal(creds)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal credentials: %v\", err)\n\t}\n\treq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error building req for endpoint [%v] : %v\", url, err)\n\t}\n\treq.Header.Add(\"Content-Type\", \"application/json\")\n\tif hc.apiKey != \"\" {\n\t\treq.Header.Set(\"Dg-Auth\", hc.apiKey)\n\t}\n\n\tresp, err := hc.doReq(req)\n\tif err != nil {\n\t\tif strings.Contains(err.Error(), \"Token is expired\") &&\n\t\t\t!strings.Contains(err.Error(), \"unable to authenticate the refresh token\") {\n\t\t\treturn hc.loginWithToken()\n\t\t}\n\t\treturn err\n\t}","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/dgraph/dgraph.go#L277-L313","documentation":"This error is returned by doLogin when json.Marshal cannot serialize the login credentials payload into JSON before POSTing it to the Dgraph /login endpoint. In practice this is nearly unreachable because creds is a plain struct with string fields, which json.Marshal can always encode; the %v verb wraps the underlying marshal error. It functions as a defensive guard in the login request-building sequence.","triggerScenarios":"json.Marshal(creds) returns a non-nil error inside doLogin. With the current creds struct (reset_password/user/password strings) this cannot occur; it would only fire if the credentials type were changed to include unsupported values (channels, funcs, or cyclic data).","commonSituations":"Developers essentially never hit this at runtime. It can surface after custom forks or refactors that replace the creds struct with types containing unsupported fields (e.g. func or chan members) or introduce cyclic references.","solutions":["Verify the credentials struct passed to loginWithCredentials contains only JSON-encodable fields (strings, numbers); no funcs, channels, or reference cycles.","If you customized the creds type, add json tags and remove unsupported field types.","Confirm you are on an unmodified version of internal/sources/dgraph/dgraph.go; rebuild from upstream sources.","If the error persists, log the wrapped %v error — it names the exact value json.Marshal failed on."],"exampleFix":"// before\ntype creds struct {\n  Callback func() `json:\"-\"` // unsupported for JSON\n  User     string\n  Password string\n}\n// after\ntype creds struct {\n  User     string `json:\"user\"`\n  Password string `json:\"password\"`\n}","handlingStrategy":"validation","validationCode":"for _, c := range []interface{}{creds} {\n  if _, err := json.Marshal(c); err != nil {\n    return fmt.Errorf(\"credentials not JSON-encodable: %w\", err)\n  }\n}","typeGuard":null,"tryCatchPattern":"if err := loginWithCredentials(ctx, user, pass); err != nil {\n  if strings.Contains(err.Error(), \"failed to marshal credentials\") {\n    // inspect the creds struct for unsupported field types\n  }\n}","preventionTips":["Keep login credential structs limited to JSON-native types (string, number, bool).","Add json struct tags to credential types and test marshalability in unit tests.","Avoid embedding funcs, channels, or cyclic pointers in config-derived structs.","Rebuild from unmodified upstream sources if this impossible error appears."],"tags":["serialization","json","dgraph","login"],"backgroundTag":"json-marshal-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"}