{"record":{"id":"1829e66a1db702d2","repo":"apache/beam","slug":"mongodbio-newreadfn-v","errorCode":null,"errorMessage":"mongodbio.newReadFn: %v","messagePattern":"mongodbio\\.newReadFn: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/read.go","lineNumber":115,"sourceCode":"\tmongoDBFn\n\tBucketAuto bool\n\tBundleSize int64\n\tFilter     []byte\n\tType       beam.EncodedType\n\tfilter     bson.M\n\tprojection bson.D\n}\n\nfunc newReadFn(\n\turi string,\n\tdatabase string,\n\tcollection string,\n\tt reflect.Type,\n\toption *ReadOption,\n) *readFn {\n\tfilter, err := encodeBSON[bson.M](option.Filter)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"mongodbio.newReadFn: %v\", err))\n\t}\n\n\treturn &readFn{\n\t\tmongoDBFn: mongoDBFn{\n\t\t\tURI:        uri,\n\t\t\tDatabase:   database,\n\t\t\tCollection: collection,\n\t\t},\n\t\tBucketAuto: option.BucketAuto,\n\t\tBundleSize: option.BundleSize,\n\t\tFilter:     filter,\n\t\tType:       beam.EncodedType{T: t},\n\t}\n}\n\nfunc (fn *readFn) Setup(ctx context.Context) error {\n\tvar err error\n\tif err = fn.mongoDBFn.Setup(ctx); err != nil {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/read.go#L97-L133","documentation":"newReadFn encodes the ReadOption's filter into BSON before building the read DoFn. If encodeBSON fails (e.g. the filter contains values not marshalable to BSON), it panics because the transform cannot be constructed. This happens at pipeline-construction time, not at runtime.","triggerScenarios":"Calling Read with a Filter option containing unsupported Go types (channels, funcs, cyclic structures) or an invalid shape that mongo's bson marshaler rejects.","commonSituations":"Building filters dynamically with unmarshaled JSON producing types bson cannot encode; embedding a struct with no BSON-marshalable fields; driver version incompatibilities with map types.","solutions":["Ensure option.Filter is a bson.M / map[string]interface{} with BSON-supported value types","Test bson.Marshal(filter) standalone before building the pipeline to reproduce the marshal error","Replace unsupported field types (e.g. time in wrong format, non-UTF8 strings, funcs)","Upgrade the mongo-driver if the marshal error is a known type-support issue"],"exampleFix":"// before\nopt.Filter = map[string]interface{}{\"ts\": someChannelOrFunc}\nmongodbio.Read(s, scope, uri, db, col, typ, opt)\n// after\nopt.Filter = bson.M{\"ts\": bson.M{\"$gte\": time.Now().Add(-time.Hour)}}\nmongodbio.Read(s, scope, uri, db, col, typ, opt)","handlingStrategy":"validation","validationCode":"if _, err := bson.Marshal(option.Filter); err != nil {\n    return fmt.Errorf(\"filter not BSON-marshalable: %w\", err)\n}","typeGuard":"func isBSONSafe(m bson.M) bool { b, err := bson.Marshal(m); return err == nil && b != nil }","tryCatchPattern":"defer func() { if r := recover(); r != nil { log.Fatalf(\"readFn construction: %v\", r) } }()","preventionTips":["Build filters only from BSON-supported types (bson.M, primitives, time.Time)","Dry-run bson.Marshal in tests for all filter shapes","Avoid embedding unmarshalable types like funcs or channels in filters"],"tags":["go","mongodb","panic","bson","marshal"],"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"}