{"record":{"id":"08a866d30e2082e4","repo":"micro/go-micro","slug":"stream-did-not-exist-and-adding-a-stream-failed","errorCode":null,"errorMessage":"Stream did not exist and adding a stream failed","messagePattern":"Stream did not exist and adding a stream failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"events/natsjs/nats.go","lineNumber":233,"sourceCode":"\t\t}\n\t}\n\n\t// ensure that a stream exists for that topic\n\t_, err := s.natsJetStreamCtx.StreamInfo(topic)\n\tif err != nil {\n\t\tcfg := &nats.StreamConfig{\n\t\t\tName: topic,\n\t\t}\n\t\tif s.opts.RetentionPolicy != 0 {\n\t\t\tcfg.Retention = nats.RetentionPolicy(s.opts.RetentionPolicy)\n\t\t}\n\t\tif s.opts.MaxAge > 0 {\n\t\t\tcfg.MaxAge = s.opts.MaxAge\n\t\t}\n\n\t\t_, err = s.natsJetStreamCtx.AddStream(cfg)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"Stream did not exist and adding a stream failed\")\n\t\t}\n\t}\n\n\t// setup the options\n\tsubOpts := []nats.SubOpt{}\n\n\tif options.CustomRetries {\n\t\tsubOpts = append(subOpts, nats.MaxDeliver(options.GetRetryLimit()))\n\t}\n\n\tif options.AutoAck {\n\t\tsubOpts = append(subOpts, nats.AckAll())\n\t} else {\n\t\tsubOpts = append(subOpts, nats.AckExplicit())\n\t}\n\n\tif !options.Offset.IsZero() {\n\t\tsubOpts = append(subOpts, nats.StartTime(options.Offset))","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/events/natsjs/nats.go#L215-L251","documentation":"Consume checks whether a JetStream stream exists for the topic via StreamInfo; if it does not, it attempts to auto-create one with AddStream. This error wraps a failure of that AddStream call, meaning the library could not find the stream and could not create it either.","triggerScenarios":"Calling Consume(topic) when no stream exists for the topic and AddStream fails — e.g. JetStream disabled on the server, insufficient account resources/limits, stream name invalid (contains characters NATS rejects), or a race where another consumer creates the same stream concurrently.","commonSituations":"NATS server started without JetStream; account storage limits exceeded; topic names with invalid characters (spaces, wildcards); two services consuming the same new topic at startup simultaneously; low disk/mem storage available on the server.","solutions":["Inspect errors.Cause(err) for the AddStream failure reason","Enable JetStream on the NATS server (nats-server -js)","Validate the topic name is a valid NATS subject/token usable as a stream name","Pre-create the stream with a proper StreamConfig (storage, retention) before consuming","Check server account JetStream limits (memory/storage) via nats account info"],"exampleFix":"// before\njs.AddStream(&nats.StreamConfig{Name: topic}) // fails: invalid stream name \"my topic\"\n// after\ntopic := strings.ReplaceAll(rawTopic, \" \", \"-\")\njs.AddStream(&nats.StreamConfig{Name: topic, Storage: nats.FileStorage})","handlingStrategy":"try-catch","validationCode":"if _, err := js.StreamInfo(topic); err != nil {\n    // stream missing; pre-create it yourself\n    _, err = js.AddStream(&nats.StreamConfig{Name: topic, Storage: nats.FileStorage})\n    if err != nil { return err }\n}","typeGuard":null,"tryCatchPattern":"ch, err := stream.Consume(topic)\nif err != nil {\n    if strings.Contains(err.Error(), \"adding a stream failed\") {\n        cause := errors.Cause(err)\n        log.Printf(\"stream creation failed: %v — check JetStream enabled & topic validity\", cause)\n    }\n    return err\n}","preventionTips":["Start nats-server with -js (JetStream enabled)","Use topic names that are valid NATS subjects and stream names","Pre-create streams with explicit storage/retention config in deployment scripts","Avoid racing multiple services to create the same stream — create once at bootstrap"],"tags":["nats","jetstream","stream","configuration"],"backgroundTag":"jetstream-stream-creation-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}