{"record":{"id":"b8826ef9c33a1409","repo":"apache/beam","slug":"bad-encode","errorCode":null,"errorMessage":"bad encode","messagePattern":"bad encode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/graph/coder/coder.go","lineNumber":151,"sourceCode":"func validateDecoder(t reflect.Type, decode any) error {\n\t// Check if it uses the real type in question.\n\tif err := funcx.Satisfy(decode, funcx.Replace(decodeSig, typex.TType, t)); err != nil {\n\t\treturn errors.WithContext(err, \"validateDecoder: validating signature\")\n\t}\n\t// TODO(lostluck): 2019.02.03 - Expand cases to avoid []byte -> any conversion\n\t// in exec, & a beam Decoder interface.\n\treturn nil\n}\n\n// NewCustomCoder creates a coder for the supplied parameters defining a\n// particular encoding strategy.\nfunc NewCustomCoder(id string, t reflect.Type, encode, decode any) (*CustomCoder, error) {\n\tif err := validateEncoder(t, encode); err != nil {\n\t\treturn nil, errors.WithContext(err, \"NewCustomCoder\")\n\t}\n\tenc, err := funcx.New(reflectx.MakeFunc(encode))\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"bad encode\")\n\t}\n\tif err := validateDecoder(t, decode); err != nil {\n\t\treturn nil, errors.WithContext(err, \"NewCustomCoder\")\n\t}\n\n\tdec, err := funcx.New(reflectx.MakeFunc(decode))\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"bad decode\")\n\t}\n\n\tc := &CustomCoder{\n\t\tName: id,\n\t\tType: t,\n\t\tEnc:  enc,\n\t\tDec:  dec,\n\t}\n\treturn c, nil\n}","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/graph/coder/coder.go#L133-L169","documentation":"NewCustomCoder wraps any error returned when converting the user-supplied encode function into an internal function value via funcx.New(reflectx.MakeFunc(encode)) with the message \"bad encode\". It means the encode function itself could not be turned into a callable reflection wrapper — typically because it is nil or not a usable function value. This happens after validateEncoder passes, so it usually indicates a structurally invalid function value rather than a signature mismatch.","triggerScenarios":"Passing a nil encode function, a non-function value, or a function whose reflect.MakeFunc conversion fails to NewCustomCoder(id, t, encode, decode).","commonSituations":"Swapping the encode and decode arguments, passing a method value obtained from a nil receiver, or passing an interface variable holding nil instead of a concrete func.","solutions":["Ensure the encode argument is a non-nil Go function with signature func(T) []byte (or compatible)","Check you did not swap the encode and decode arguments to NewCustomCoder","Inspect the wrapped underlying error in the errors.Wrap chain for the exact funcx failure","If passing a method value, verify the receiver is non-nil"],"exampleFix":"// before\ncc, err := coder.NewCustomCoder(\"myCoder\", reflect.TypeOf(x), nil, decodeFn)\n// after\ncc, err := coder.NewCustomCoder(\"myCoder\", reflect.TypeOf(x), func(v T) []byte { return encode(v) }, decodeFn)","handlingStrategy":"validation","validationCode":"if encode == nil { return errors.New(\"encode must be a non-nil func\") }\nif rv := reflect.ValueOf(encode); rv.Kind() != reflect.Func { return fmt.Errorf(\"encode must be a func, got %v\", rv.Kind()) }","typeGuard":"func isFunc(v any) bool { return v != nil && reflect.ValueOf(v).Kind() == reflect.Func }","tryCatchPattern":null,"preventionTips":["Never pass nil or zero-value function variables to NewCustomCoder","Double-check argument order (encode first, decode second)","Validate the encoder matches func(T) []byte before registering"],"tags":["go","beam","custom-coder","reflection"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}