{"record":{"id":"9ee379cd8cb74db2","repo":"googleapis/mcp-toolbox","slug":"could-not-unmarshal-operation-w","errorCode":null,"errorMessage":"could not unmarshal operation: %w","messagePattern":"could not unmarshal operation: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqladmin/cloud_sql_admin.go","lineNumber":370,"sourceCode":"\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) {\n\tbackupRun := &sqladmin.BackupRun{}\n\tif location != \"\" {\n\t\tbackupRun.Location = location\n\t}\n\tif backupDescription != \"\" {","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqladmin/cloud_sql_admin.go#L352-L388","documentation":"After successfully marshaling a DONE operation, the library unmarshals the bytes into map[string]any so generateCloudSQLConnectionMessage can extract connection details. If json.Unmarshal fails on bytes that were just marshaled from a sqladmin.Operation, this error is thrown. This should be unreachable with valid operation data and signals a corrupted payload or a marshaling/serialization mismatch.","triggerScenarios":"op.MarshalJSON() succeeded but produced bytes that json.Unmarshal into map[string]any rejects — e.g. malformed JSON from a custom/broken MarshalJSON implementation or a struct containing a function/invalid field rendered incorrectly.","commonSituations":"Essentially only seen with a broken or mismatched google.golang.org/api version, patched client code, or intercepting proxies that corrupt the operation JSON before it reaches local marshaling.","solutions":["Upgrade google.golang.org/api to the latest version","Log opBytes content on failure to inspect what was produced and why it cannot unmarshal","Bypass the map conversion: pass opBytes or a typed struct to the connection-message generator instead","Verify no proxy/middleware is transforming the API response payload"],"exampleFix":"// before\nvar data map[string]any\nif err := json.Unmarshal(opBytes, &data); err != nil {\n    return nil, fmt.Errorf(\"could not unmarshal operation: %w\", err)\n}\n// after\nvar data map[string]any\nif err := json.Unmarshal(opBytes, &data); err != nil {\n    return nil, fmt.Errorf(\"could not unmarshal operation %s: %w (raw: %s)\", operation, err, string(opBytes))\n}","handlingStrategy":"try-catch","validationCode":"// Verify the marshaled operation bytes decode into a map before downstream use\nfunc operationDecodable(opBytes []byte) error {\n    var probe map[string]any\n    if err := json.Unmarshal(opBytes, &probe); err != nil {\n        return fmt.Errorf(\"operation bytes not decodable: %w\", err)\n    }\n    return nil\n}","typeGuard":"func decodeOperation(opBytes []byte) (map[string]any, bool) {\n    var data map[string]any\n    if err := json.Unmarshal(opBytes, &data); err != nil {\n        return nil, false\n    }\n    return data, true\n}","tryCatchPattern":"result, err := src.GetWaitForOperations(ctx, service, project, opName, tpl, delay)\nif err != nil && strings.Contains(err.Error(), \"could not unmarshal operation\") {\n    // Graceful fallback: return the raw operation JSON via REST\n    resp, rerr := service.Operations.Get(project, opName).Do()\n    if rerr == nil {\n        raw, _ := json.Marshal(resp)\n        fmt.Println(string(raw))\n    }\n    return\n}","preventionTips":["Update google.golang.org/api; a MarshalJSON/Unmarshal mismatch is a library bug, not user error","Avoid proxies/interceptors that rewrite JSON payloads between the API and the client","Add raw-JSON fallback logging so operation results are never lost to a decode failure"],"tags":["gcp","cloudsql","json-unmarshal","operations"],"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"}