micro/go-micro · error
Stream did not exist and adding a stream failed
Error message
Stream did not exist and adding a stream failed
What it means
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.
Source
Thrown at events/natsjs/nats.go:233
}
}
// ensure that a stream exists for that topic
_, err := s.natsJetStreamCtx.StreamInfo(topic)
if err != nil {
cfg := &nats.StreamConfig{
Name: topic,
}
if s.opts.RetentionPolicy != 0 {
cfg.Retention = nats.RetentionPolicy(s.opts.RetentionPolicy)
}
if s.opts.MaxAge > 0 {
cfg.MaxAge = s.opts.MaxAge
}
_, err = s.natsJetStreamCtx.AddStream(cfg)
if err != nil {
return nil, errors.Wrap(err, "Stream did not exist and adding a stream failed")
}
}
// setup the options
subOpts := []nats.SubOpt{}
if options.CustomRetries {
subOpts = append(subOpts, nats.MaxDeliver(options.GetRetryLimit()))
}
if options.AutoAck {
subOpts = append(subOpts, nats.AckAll())
} else {
subOpts = append(subOpts, nats.AckExplicit())
}
if !options.Offset.IsZero() {
subOpts = append(subOpts, nats.StartTime(options.Offset))View on GitHub (pinned to 24529f1404)
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
Example fix
// before
js.AddStream(&nats.StreamConfig{Name: topic}) // fails: invalid stream name "my topic"
// after
topic := strings.ReplaceAll(rawTopic, " ", "-")
js.AddStream(&nats.StreamConfig{Name: topic, Storage: nats.FileStorage}) Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := js.StreamInfo(topic); err != nil {
// stream missing; pre-create it yourself
_, err = js.AddStream(&nats.StreamConfig{Name: topic, Storage: nats.FileStorage})
if err != nil { return err }
} Try / catch
ch, err := stream.Consume(topic)
if err != nil {
if strings.Contains(err.Error(), "adding a stream failed") {
cause := errors.Cause(err)
log.Printf("stream creation failed: %v — check JetStream enabled & topic validity", cause)
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- addr (nats subject) must not be empty
- source not found: %s
- error connecting to nats cluster %v: %w
- error while obtaining JetStream context: %w
- Error subscribing to topic
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/08a866d30e2082e4.
Report an issue: GitHub.