{"record":{"id":"915ce4240274a251","repo":"kubernetes/kops","slug":"error-converting-to-yaml-v","errorCode":null,"errorMessage":"error converting to yaml: %v","messagePattern":"error converting to yaml: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apis/kops/parse.go","lineNumber":78,"sourceCode":"\tconfigString := string(data)\n\tconfigString = strings.TrimSpace(configString)\n\n\tif configString != \"\" {\n\t\terr := yaml.Unmarshal([]byte(configString), dest, yaml.DisallowUnknownFields)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error parsing configuration: %v\", err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// ToRawYaml marshals an object to yaml, without the full api machinery\n// Deprecated: prefer using the API machinery (package kopscodecs)\nfunc ToRawYaml(obj interface{}) ([]byte, error) {\n\tdata, err := utils.YamlMarshal(obj)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error converting to yaml: %v\", err)\n\t}\n\n\treturn data, nil\n}\n","sourceCodeStart":60,"sourceCodeEnd":83,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/apis/kops/parse.go#L60-L83","documentation":"ToRawYaml marshals an arbitrary Go object to YAML bytes using utils.YamlMarshal and wraps any marshal failure in this error. Failures are rare because YAML marshalling of plain structs/slices seldom fails, but unsupported types (e.g. channels, funcs, cyclic pointers) or an internal encoder error will trigger it. The function is deprecated in favor of the kopscodecs API machinery.","triggerScenarios":"Calling ToRawYaml with an object containing a value the YAML encoder cannot serialize — channels, function values, cyclic data structures, or invalid map keys — from callers like RunToolboxDump, writeAuthenticationConfig, or task validation in tests.","commonSituations":"Dumping a dynamically-built object that accidentally holds a non-serializable value; a struct field of unsupported type added by a recent code change; marshalling an empty/nil interface from an uninitialized config in test helpers like ValidateTasks.","solutions":["Inspect the wrapped %v error to find the non-serializable value; remove or replace that field.","Follow the deprecation notice: switch to the kopscodecs API machinery (serialization via the typed API) instead of ToRawYaml.","If marshalling a generated/dynamic object, sanitize it (e.g. convert to a plain map or typed struct) before marshalling.","Nil-check the object and ensure initialized, typed values rather than raw interfaces."],"exampleFix":"// before\nout, err := kops.ToRawYaml(objWithChannel) // unsupported field\n// after\nobjWithChannel.Ch = nil // or use kopscodecs serialization\nout, err := kopscodecs.ToYAML(objWithChannel)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"data, err := kops.ToRawYaml(obj)\nif err != nil {\n\treturn fmt.Errorf(\"object %T not yaml-serializable: %w\", obj, err)\n}","preventionTips":["Prefer the kopscodecs API machinery over the deprecated ToRawYaml.","Avoid fields of type chan, func, or recursive pointers in objects you serialize.","Build config objects from typed structs, not loose interface{} maps.","Add a unit test that round-trips (marshal then unmarshal) every config object you dump."],"tags":["yaml","serialization","kops"],"backgroundTag":"yaml-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}