{"record":{"id":"266a75150bdd0178","repo":"apache/beam","slug":"err-coder","errorCode":null,"errorMessage":"err","messagePattern":"err","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/coder.go","lineNumber":111,"sourceCode":"// encoding: encoding two equal values always yields identical byte\n// sequences.\n//\n// Determinism is required for any coder used as a state key in a stateful\n// DoFn or as the key component of a KV consumed by GroupByKey /\n// GroupIntoBatches. A non-deterministic key coder would silently corrupt\n// state keying, splintering state across apparently-distinct keys.\nfunc (c Coder) IsDeterministic() bool {\n\tif c.coder == nil {\n\t\treturn false\n\t}\n\treturn c.coder.IsDeterministic()\n}\n\n// NewElementEncoder returns a new encoding function for the given type.\nfunc NewElementEncoder(t reflect.Type) ElementEncoder {\n\tc, err := inferCoder(typex.New(t))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn &execEncoder{enc: exec.MakeElementEncoder(c)}\n}\n\n// execEncoder wraps an exec.ElementEncoder to implement the ElementDecoder interface\n// in this package.\ntype execEncoder struct {\n\tenc   exec.ElementEncoder\n\tcoder *coder.Coder\n}\n\nfunc (e *execEncoder) Encode(element any, w io.Writer) error {\n\treturn e.enc.Encode(&exec.FullValue{Elm: element}, w)\n}\n\nfunc (e *execEncoder) String() string {\n\treturn e.coder.String()\n}","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/coder.go#L93-L129","documentation":"beam.NewElementEncoder infers a coder for the given reflect.Type and panics on any inference error, surfacing the raw error as the panic value (message 'err'). It is a convenience API that treats coder inference failure as a non-recoverable programming error.","triggerScenarios":"Passing a reflect.Type whose element type the SDK cannot infer a coder for — e.g. an unsupported container (chan, func), unregistered custom struct with unsupported fields, or interface types.","commonSituations":"Encoding channels, function values, or types containing them; custom types without registered coders; complex nested generics the inference rules don't cover.","solutions":["Register a custom coder for the type via beam.RegisterCoder before calling NewElementEncoder","Use only supported types (primitives, slices, maps, arrays, structs of supported types, KV, etc.)","Change the calling code to use TryInfer or capture the error path instead of the panicking convenience wrapper","Inspect the panic value for the specific inference failure reason"],"exampleFix":"// before\nenc := beam.NewElementEncoder(reflect.TypeOf(make(chan int)))\n// after\ntypex.RegisterCoder(reflect.TypeOf(chanInt(0)), encodeChan, decodeChan)\nenc := beam.NewElementEncoder(reflect.TypeOf(chanInt(0)))","handlingStrategy":"try-catch","validationCode":"switch t.Kind() {\ncase reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Interface:\n\treturn errors.New(\"unsupported type for element encoder\")\n}","typeGuard":"func encodable(t reflect.Type) bool {\n\tswitch t.Kind() {\n\tcase reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Interface:\n\t\treturn false\n\t}\n\treturn true\n}","tryCatchPattern":"func safeEncoder(t reflect.Type) (enc beam.ElementEncoder, err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = fmt.Errorf(\"coder inference failed: %v\", r)\n\t\t}\n\t}()\n\treturn beam.NewElementEncoder(t), nil\n}","preventionTips":["Register custom coders for non-standard types before use","Restrict encoded types to SDK-supported kinds","Prefer error-returning inference helpers over panicking convenience wrappers in library code"],"tags":["go","apache-beam","panic","coder","reflection"],"backgroundTag":"unsupported-operation","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"}