{"record":{"id":"36aa43ffc4150ecb","repo":"gohugoio/hugo","slug":"symdiff-failed-to-convert-value-w","errorCode":null,"errorMessage":"symdiff: failed to convert value: %w","messagePattern":"symdiff: failed to convert value: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/symdiff.go","lineNumber":57,"sourceCode":"\t\tv := reflect.ValueOf(s)\n\n\t\tswitch v.Kind() {\n\t\tcase reflect.Array, reflect.Slice:\n\t\t\tif i == 0 {\n\t\t\t\tsliceType := v.Type()\n\t\t\t\tsliceElemType = sliceType.Elem()\n\t\t\t\tslice = reflect.MakeSlice(sliceType, 0, 0)\n\t\t\t}\n\n\t\t\tfor i := range v.Len() {\n\t\t\t\tev, _ := hreflect.Indirect(v.Index(i))\n\t\t\t\tkey := normalize(ev)\n\n\t\t\t\t// Append if the key is not in their intersection.\n\t\t\t\tif ids1[key] != ids2[key] {\n\t\t\t\t\tv, err := convertValue(ev, sliceElemType)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf(\"symdiff: failed to convert value: %w\", err)\n\t\t\t\t\t}\n\t\t\t\t\tslice = reflect.Append(slice, v)\n\t\t\t\t}\n\t\t\t}\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"arguments to symdiff must be slices or arrays\")\n\t\t}\n\t}\n\n\treturn slice.Interface(), nil\n}\n","sourceCodeStart":39,"sourceCodeEnd":69,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/symdiff.go#L39-L69","documentation":"Wrapping error from SymDiff when convertValue fails while converting an element to the result slice's element type. The %w verb wraps the underlying convertValue error (which may itself be the 'is not assignable' or 'unable to convert' message). This is the user-facing surface for element-coercion failures during symmetric difference computation.","triggerScenarios":"Calling `{{ symdiff $a $b }}` where elements of $a and $b cannot be coerced to a common type — e.g. []string vs []int, or slices of incompatible structs. convertValue returns an error inside the SymDiff loop and it is wrapped here.","commonSituations":"Author assumes two slices are element-compatible when they are not, or data sources produce differently-typed slices (JSON numbers vs strings). Common when one slice comes from front matter and another from a data file.","solutions":["Read the wrapped error to find the exact conversion failure (assignable vs numeric).","Normalize both slices to the same element type before calling symdiff.","Map elements to strings or numbers upstream.","Verify element types match across all data sources."],"exampleFix":"// before\n{{ symdiff $stringSlice $intSlice }}\n// after\n{{ symdiff $stringSliceA $stringSliceB }}","handlingStrategy":"try-catch","validationCode":"func symdiffSafe(a, b any) (any, error) {\n    ta := reflect.TypeOf(a); tb := reflect.TypeOf(b)\n    if ta == nil || tb == nil { return nil, errors.New(\"nil operand\") }\n    if ta.Kind() != reflect.Slice && ta.Kind() != reflect.Array { return nil, fmt.Errorf(\"a must be slice/array\") }\n    if tb.Kind() != reflect.Slice && tb.Kind() != reflect.Array { return nil, fmt.Errorf(\"b must be slice/array\") }\n    if ta.Elem() != tb.Elem() && !elementTypesCompatible(ta.Elem(), tb.Elem()) {\n        return nil, fmt.Errorf(\"element types incompatible: %s vs %s\", ta.Elem(), tb.Elem())\n    }\n    return ns.SymDiff(a, b)\n}","typeGuard":"func symdiffCompatible(a, b any) bool {\n    ta, tb := reflect.TypeOf(a), reflect.TypeOf(b)\n    if ta == nil || tb == nil { return false }\n    if ta.Kind() != reflect.Slice && ta.Kind() != reflect.Array { return false }\n    if tb.Kind() != reflect.Slice && tb.Kind() != reflect.Array { return false }\n    return ta.Elem() == tb.Elem() || elementTypesCompatible(ta.Elem(), tb.Elem())\n}","tryCatchPattern":"r, err := ns.SymDiff(s2, s1)\nif err != nil {\n    // err wraps the conversion failure; unwrap for the root cause\n    return nil, fmt.Errorf(\"symdiff failed: %w\", err)\n}","preventionTips":["Normalize both slices to the same element type before symdiff.","Read the wrapped error to find the exact conversion failure.","Map elements to strings or numbers upstream.","Verify element type compatibility across all data sources."],"tags":["go","hugo","template","collections","symdiff","conversion","wrapper-error"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}