{"record":{"id":"be8b173d3eb5967a","repo":"apache/beam","slug":"mongodbio-write-invalid-option-v","errorCode":null,"errorMessage":"mongodbio.Write: invalid option: %v","messagePattern":"mongodbio\\.Write: invalid option: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/write.go","lineNumber":86,"sourceCode":"//   - Ordered: whether to execute the writes in order. Defaults to true\nfunc Write(\n\ts beam.Scope,\n\turi string,\n\tdatabase string,\n\tcollection string,\n\tcol beam.PCollection,\n\topts ...WriteOptionFn,\n) beam.PCollection {\n\ts = s.Scope(\"mongodbio.Write\")\n\n\toption := &WriteOption{\n\t\tBatchSize: defaultWriteBatchSize,\n\t\tOrdered:   defaultWriteOrdered,\n\t}\n\n\tfor _, opt := range opts {\n\t\tif err := opt(option); err != nil {\n\t\t\tpanic(fmt.Sprintf(\"mongodbio.Write: invalid option: %v\", err))\n\t\t}\n\t}\n\n\tt := col.Type().Type()\n\tidIndex := structx.FieldIndexByTag(t, bsonTag, \"_id\")\n\n\tvar keyed beam.PCollection\n\n\tif idIndex == -1 {\n\t\tpre := beam.ParDo(s, createIDFn, col)\n\t\tkeyed = beam.Reshuffle(s, pre)\n\t} else {\n\t\tkeyed = beam.ParDo(\n\t\t\ts,\n\t\t\tnewExtractIDFn(idIndex),\n\t\t\tcol,\n\t\t\tbeam.TypeDefinition{Var: beam.XType, T: t.Field(idIndex).Type},\n\t\t)","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/write.go#L68-L104","documentation":"mongodbio.Write validates each variadic WriteOption by invoking it; if an option func returns an error, Write panics at graph-construction time because options are expected to be statically valid. Note Write also infers the _id field index from the element type via structx tags right after.","triggerScenarios":"Passing a custom func(*WriteOption) error that returns non-nil, e.g. a WithBatchSize helper rejecting non-positive batch sizes or an invalid Ordered combination, when calling Write.","commonSituations":"Options sourced from config flags (batch size 0 or negative); custom option funcs with buggy validation; conflicting options like generate-ID plus an existing _id tag mishandled.","solutions":["Fix the invalid input the option validates (positive BatchSize, sane Ordered flag)","Inspect custom WriteOption funcs for validation bugs","Validate batch size / ordering config before calling Write","Wrap construction in recover() to surface the message cleanly"],"exampleFix":"// before\nopt := func(o *mongodbio.WriteOption) error {\n    o.BatchSize = cfg.BatchSize // 0\n    return nil\n}\n// after\nopt := func(o *mongodbio.WriteOption) error {\n    if cfg.BatchSize <= 0 {\n        return fmt.Errorf(\"batchSize must be > 0, got %d\", cfg.BatchSize)\n    }\n    o.BatchSize = cfg.BatchSize\n    return nil\n}","handlingStrategy":"validation","validationCode":"func validateWriteOption(o *mongodbio.WriteOption) error {\n    if o.BatchSize <= 0 { return fmt.Errorf(\"BatchSize must be > 0\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"defer func() { if r := recover(); r != nil { log.Fatalf(\"Write options invalid: %v\", r) } }()","preventionTips":["Validate batch-size/ordering flags before building the pipeline","Test custom WriteOption funcs like any other code","Prefer library option constructors over ad-hoc closures"],"tags":["go","mongodb","panic","options"],"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-14T16:17:12.679Z"}