{"record":{"id":"7d34f2cce6390752","repo":"apache/beam","slug":"error-encoding-bson-w","errorCode":null,"errorMessage":"error encoding BSON: %w","messagePattern":"error encoding BSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/coder.go","lineNumber":65,"sourceCode":"\nfunc encodeRestriction(in idRangeRestriction) ([]byte, error) {\n\treturn encodeBSON(in)\n}\nfunc decodeRestriction(in []byte) (idRangeRestriction, error) {\n\treturn decodeBSON[idRangeRestriction](in)\n}\n\nfunc encodeRange(in idRange) ([]byte, error) {\n\treturn encodeBSON(in)\n}\nfunc decodeRange(in []byte) (idRange, error) {\n\treturn decodeBSON[idRange](in)\n}\n\nfunc encodeBSON[T any](in T) ([]byte, error) {\n\tout, err := bson.Marshal(in)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error encoding BSON: %w\", err)\n\t}\n\n\treturn out, nil\n}\n\nfunc decodeBSON[T any](in []byte) (T, error) {\n\tvar out T\n\tif err := bson.Unmarshal(in, &out); err != nil {\n\t\treturn out, fmt.Errorf(\"error decoding BSON: %w\", err)\n\t}\n\n\treturn out, nil\n}\n\nfunc encodeObjectID(objectID primitive.ObjectID) []byte {\n\treturn objectID[:]\n}\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/coder.go#L47-L83","documentation":"Returned by the generic helper encodeBSON in mongodbio when bson.Marshal fails to serialize a value (restriction or range for the MongoDB splitter) into BSON bytes. This indicates the Go struct or value passed to the encoder is not BSON-encodable.","triggerScenarios":"Calling encodeBSON (via encodeRestriction/encodeRange) with a type containing unsupported fields, e.g. channels, funcs, maps with non-string keys, nil interfaces, or invalid NaN values.","commonSituations":"Adding custom fields to restriction/range structs after a Beam version upgrade; embedding non-serializable types; primitive type mismatches after upgrading the mongo-driver major version.","solutions":["Inspect the wrapped bson error to find the offending field and change its type to a BSON-encodable one.","Add bson struct tags to ensure all fields map to valid BSON types.","Ensure restriction/range structs only contain primitives, strings, time.Time, and ObjectIDs.","Check for mongo-driver version incompatibilities after dependency upgrades."],"exampleFix":"// before: unencodable field\nsplitFn struct { done chan struct{}; start int64 }\n\n// after: only BSON-friendly fields\nsplitFn struct { done bool; start int64 }","handlingStrategy":"try-catch","validationCode":"// keep restriction/range structs BSON-safe:\n// only string, ints, floats, bool, time.Time, primitive.ObjectID, slices; no chans/funcs/nil interfaces\nfunc bsonSafe(v any) error { _, err := bson.Marshal(v); return err }","typeGuard":null,"tryCatchPattern":"encoded, err := encodeBSON(r)\nif err != nil {\n    return fmt.Errorf(\"encoding split restriction: %w\", err)\n}","preventionTips":["Keep custom restriction/range fields restricted to BSON-encodable primitives.","Add bson:\"...\" struct tags to every field you control.","Run bson.Marshal round-trip tests on restriction structs in CI.","Re-check encodability after upgrading go.mongodb.org/mongo-driver major versions."],"tags":["mongodb","bson","serialization","go"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}