micro/go-micro · error

ErrMissingTopic

ErrMissingTopic

Error message

missing topic

What it means

ErrMissingTopic is the sentinel error returned by the events package when an empty topic string is supplied. Topics identify event streams, so publish, consume, and read all require a non-blank topic to route the message.

Source

Thrown at events/events.go:19

// Package events is for event streaming and storage
package events

import (
	"encoding/json"
	"errors"
	"time"
)

var (
	// DefaultStream is the default events stream implementation
	DefaultStream Stream
	// DefaultStore is the default events store implementation
	DefaultStore Store
)

var (
	// ErrMissingTopic is returned if a blank topic was provided to publish
	ErrMissingTopic = errors.New("missing topic")
	// ErrEncodingMessage is returned from publish if there was an error encoding the message option
	ErrEncodingMessage = errors.New("error encoding message")
)

// Stream is an event streaming interface
type Stream interface {
	Publish(topic string, msg interface{}, opts ...PublishOption) error
	Consume(topic string, opts ...ConsumeOption) (<-chan Event, error)
}

// Store is an event store interface
type Store interface {
	Read(topic string, opts ...ReadOption) ([]*Event, error)
	Write(event *Event, opts ...WriteOption) error
}

type AckFunc func() error
type NackFunc func() error

View on GitHub (pinned to 24529f1404)

Solutions

  1. Check for the sentinel before use: if errors.Is(err, events.ErrMissingTopic), log which call site supplied the blank topic.
  2. Validate topic non-empty before calling Publish/Consume and fail fast with the topic's config key name in the message.
  3. Provide a default topic in configuration or derive it from the service name when unset.
  4. Fix upstream wiring so the topic variable is populated (env var, config file, constructor argument).

Example fix

// before
topic := os.Getenv("EVENTS_TOPIC")
stream.Publish(topic, msg) // ErrMissingTopic if unset
// after
topic := os.Getenv("EVENTS_TOPIC")
if topic == "" {
    topic = "default-events"
}
stream.Publish(topic, msg)
Defensive patterns

Strategy: validation

Validate before calling

if topic == "" {
    return events.ErrMissingTopic
}

Try / catch

if err := stream.Publish(topic, msg); err != nil {
    if errors.Is(err, events.ErrMissingTopic) {
        return fmt.Errorf("publish: topic not configured: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Stream.Publish("", msg), Stream.Consume("", ...), or Store.Read(topic="") with a topic variable that is the empty string (unset config value, uninitialized struct field).

Common situations: A topic configured via env var/config file that isn't set in one environment; a struct field of topic names zero-valued; string interpolation that yields an empty topic (e.g. fmt.Sprintf with empty parts).

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/b2fdea7d27ca9554. Report an issue: GitHub.