{"record":{"id":"d1c610993f5e5a10","repo":"googleapis/mcp-toolbox","slug":"could-not-marshal-operation-w-d1c610","errorCode":null,"errorMessage":"could not marshal operation: %w","messagePattern":"could not marshal operation: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqladmin/cloud_sql_admin.go","lineNumber":365,"sourceCode":"\t}\n\top, err := service.Operations.Get(project, operation).Do()\n\tif err != nil {\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"error getting operation: %s, retrying in %v\", err, delay))\n\t} else {\n\t\tif op.Status == \"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\tvar data map[string]any\n\t\t\tif err := json.Unmarshal(opBytes, &data); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"could not unmarshal operation: %w\", err)\n\t\t\t}\n\n\t\t\tif msg, ok := generateCloudSQLConnectionMessage(ctx, s, logger, data, connectionMessageTemplate); ok {\n\t\t\t\treturn msg, nil\n\t\t\t}\n\t\t\treturn string(opBytes), nil\n\t\t}\n\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"operation not complete, retrying in %v\", delay))\n\t}\n\treturn nil, nil\n}\n\nfunc (s *Source) InsertBackupRun(ctx context.Context, project, instance, location, backupDescription, accessToken string) (any, error) {","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqladmin/cloud_sql_admin.go#L347-L383","documentation":"After a successful DONE operation, the library calls op.MarshalJSON() to serialize the full operation object for the response. If that built-in marshaling fails, this error is thrown. In practice this is near-impossible with stock sqladmin.Operation types unless the API client library is mismatched or the object holds unusual ForceSendFields/NullFields state.","triggerScenarios":"service.Operations.Get returned a DONE operation with no op.Error, but op.MarshalJSON() returned a non-nil error during result serialization in GetWaitForOperations.","commonSituations":"Version incompatibility between the generated sqladmin API types and the serialization helpers, corrupted client state, or unusual operation payloads from newer API versions not understood by an older google.golang.org/api version.","solutions":["Upgrade the google.golang.org/api dependency to the latest version","Fall back to json.Marshal(op) in the handler to capture and inspect the failure","Log the operation name and retrieve it via gcloud sql operations describe instead of in-process marshaling","Pin consistent versions of google.golang.org/api across the build to avoid generated-type mismatches"],"exampleFix":"// before\nopBytes, err = op.MarshalJSON()\nif err != nil {\n    return nil, fmt.Errorf(\"could not marshal operation: %w\", err)\n}\n// after\nopBytes, err = op.MarshalJSON()\nif err != nil {\n    logger.WarnContext(ctx, fmt.Sprintf(\"op.MarshalJSON failed (%v), falling back to json.Marshal\", err))\n    if opBytes, err = json.Marshal(op); err != nil {\n        return nil, fmt.Errorf(\"could not marshal operation %s: %w\", op.Name, err)\n    }\n}","handlingStrategy":"fallback","validationCode":"// Pre-check that the operation object serializes cleanly right after fetch\nfunc operationSerializable(op *sqladmin.Operation) error {\n    if _, err := op.MarshalJSON(); err != nil {\n        return fmt.Errorf(\"operation %s not serializable: %w\", op.Name, err)\n    }\n    return nil\n}","typeGuard":"func canMarshalOperation(op *sqladmin.Operation) ([]byte, bool) {\n    b, err := op.MarshalJSON()\n    if err != nil {\n        if b2, err2 := json.Marshal(op); err2 == nil {\n            return b2, true\n        }\n        return nil, false\n    }\n    return b, true\n}","tryCatchPattern":"result, err := src.GetWaitForOperations(ctx, service, project, opName, tpl, delay)\nif err != nil && strings.Contains(err.Error(), \"could not marshal operation\") {\n    // fallback: fetch and describe via CLI or raw HTTP\n    out, cmdErr := exec.Command(\"gcloud\", \"sql\", \"operations\", \"describe\", opName, \"--project\", project, \"--format=json\").Output()\n    if cmdErr == nil {\n        fmt.Println(string(out))\n    }\n    return\n}","preventionTips":["Pin and regularly update google.golang.org/api to avoid generated-type serialization bugs","Avoid custom wrapping or field mutation of sqladmin.Operation before serialization","Have an out-of-band fallback (gcloud / REST API) to retrieve operation results"],"tags":["gcp","cloudsql","json-marshal","operations"],"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"}