{"record":{"id":"f5c2d485c465120c","repo":"googleapis/mcp-toolbox","slug":"could-not-marshal-operation-w","errorCode":null,"errorMessage":"could not marshal operation: %w","messagePattern":"could not marshal operation: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/alloydbadmin/alloydbadmin.go","lineNumber":357,"sourceCode":"\n\top, err := service.Projects.Locations.Operations.Get(name).Do()\n\tif err != nil {\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"error getting operation: %s, retrying in %v\\n\", err, delay))\n\t} else {\n\t\tif op.Done {\n\t\t\tif op.Error != nil {\n\t\t\t\tvar errorBytes []byte\n\t\t\t\terrorBytes, err = json.Marshal(op.Error)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, fmt.Errorf(\"operation finished with error but could not marshal error object: %w\", err)\n\t\t\t\t}\n\t\t\t\treturn nil, fmt.Errorf(\"operation finished with error: %s\", string(errorBytes))\n\t\t\t}\n\n\t\t\tvar opBytes []byte\n\t\t\topBytes, err = op.MarshalJSON()\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"could not marshal operation: %w\", err)\n\t\t\t}\n\n\t\t\tif op.Response != nil {\n\t\t\t\tvar responseData map[string]any\n\t\t\t\tif err := json.Unmarshal(op.Response, &responseData); err == nil && responseData != nil {\n\t\t\t\t\tif msg, ok := generateAlloyDBConnectionMessage(responseData, connectionMessageTemplate); ok {\n\t\t\t\t\t\treturn msg, nil\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tvar result any\n\t\t\tif err := json.Unmarshal(opBytes, &result); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to unmarshal operation bytes: %w\", err)\n\t\t\t}\n\t\t\treturn result, nil\n\t\t}\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"Operation not complete, retrying in %v\\n\", delay))","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/alloydbadmin/alloydbadmin.go#L339-L375","documentation":"GetOperations polls a long-running AlloyDB Admin operation and, once complete, serializes the operation object to JSON via op.MarshalJSON(). This error wraps any failure during that serialization. It means the completed operation object could not be converted to its JSON representation, so GetOperations cannot return a result.","triggerScenarios":"Calling GetOperations on an AlloyDB Admin operation whose LRO struct, once fetched in DONE state, fails MarshalJSON — e.g. an operation response/metadata payload containing types the generated client cannot serialize (unexpected field types or nil-unsafe marshaling).","commonSituations":"Unusual operation payloads returned by the AlloyDB Admin API (error operations with atypical metadata), or bugs/mismatches between the generated longrunning client version and the actual API response shape.","solutions":["Retry GetOperations; transient serialization of a malformed fetch is unlikely but the underlying operation may have been fetched incompletely — re-fetch and retry.","Inspect the wrapped err (%w) to identify which field failed to marshal.","Check the AlloyDB Admin API operation directly (gcloud alloydb operations describe) to see if the operation payload is malformed.","Upgrade google.golang.org/alloydb/admin/apiv1alpha (or related) client libraries to the latest version.","File an issue with the operation name if the API returns a payload the SDK cannot serialize."],"exampleFix":"// before\nopBytes, err := op.MarshalJSON()\nif err != nil { return nil, fmt.Errorf(\"could not marshal operation: %w\", err) }\n// after\nopBytes, err := op.MarshalJSON()\nif err != nil {\n    logger.DebugContext(ctx, fmt.Sprintf(\"operation %s failed to marshal: %v\", opName, err))\n    return nil, fmt.Errorf(\"could not marshal operation: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// Prefetch and inspect the operation via the Admin API before calling GetOperations\nop, err := adminClient.GetOperation(ctx, &adminpb.GetOperationRequest{Name: opName})\nif err != nil { return err }\nif !op.Done { return fmt.Errorf(\"operation %s not done yet\", opName) }","typeGuard":null,"tryCatchPattern":"result, err := toolbox.GetOperations(ctx, opName)\nif err != nil {\n    if strings.Contains(err.Error(), \"could not marshal operation\") {\n        // inspect op via gcloud / retry once\n    }\n    return fmt.Errorf(\"get operations failed: %w\", err)\n}","preventionTips":["Keep the AlloyDB Admin Go SDK up to date","Log the operation name on failure for support/debugging","Validate the operation reaches DONE without error before processing","Pin and test SDK versions against your AlloyDB API revision"],"tags":["go","serialization","alloydb","long-running-operation"],"backgroundTag":"json-serialization-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"}