Billionmail/BillionMail · error

key cannot be empty

Error message

key cannot be empty

What it means

OptionsMgr.SetOption in core/internal/service/public/options_mgr.go stores a key/value option, serializing the value as JSON. It rejects an empty key with "key cannot be empty" as a fail-fast guard. This is a caller validation error — the option is never written.

Source

Thrown at core/internal/service/public/options_mgr.go:32

	cache      *gcache.Cache // Cache instance
	expiration time.Duration // Cache expiration time
}

// NewOptionsMgr Create options manager
func NewOptionsMgr() *OptionsMgr {
	return &OptionsMgr{
		cache:      gcache.New(),
		expiration: time.Hour * 24, // Default 24 hours cache
	}
}

// OptionsMgrInstance Options manager singleton
var OptionsMgrInstance = NewOptionsMgr()

// SetOption Set option
func (o *OptionsMgr) SetOption(ctx context.Context, key string, value interface{}) error {
	if key == "" {
		return errors.New("key cannot be empty")
	}

	if value == nil {
		return errors.New("value cannot be nil")
	}

	// Serialize value
	jsonValue, err := o.serialize(value)
	if err != nil {
		return err
	}

	// Save to database
	if err := o.saveToDatabase(ctx, key, jsonValue); err != nil {
		return err
	}

	// Update cache

View on GitHub (pinned to fc36c76c05)

Solutions

  1. Ensure a non-empty key is supplied; check the constant/variable used for the key name.
  2. Add upstream validation on the API/controller layer to reject empty option keys with a clear message.
  3. Log the call site to find where the empty key originates (often a missing default or a map miss).

Example fix

// before
key := cfg.OptionName // ""
OptionsMgrInstance.SetOption(ctx, key, val) // key cannot be empty
// after
if strings.TrimSpace(cfg.OptionName) == "" {
    return gerror.New("option name is required")
}
return OptionsMgrInstance.SetOption(ctx, cfg.OptionName, val)
Defensive patterns

Strategy: validation

Validate before calling

if strings.TrimSpace(key) == "" {
    return errors.New("option key is required")
}
err := OptionsMgrInstance.SetOption(ctx, key, value)

Type guard

func hasKey(key string) bool { return strings.TrimSpace(key) != "" }

Try / catch

if err := OptionsMgrInstance.SetOption(ctx, key, value); err != nil {
    if err.Error() == "key cannot be empty" {
        return fmt.Errorf("option name missing at %s — check the constant or request field", caller)
    }
    return err
}

Prevention

When it happens

Trigger: Calling OptionsMgrInstance.SetOption(ctx, "", value) — usually the key came from an unvalidated request field, a missing map entry, or a variable that failed to initialize to a non-empty string.

Common situations: Settings UI posting an option whose name field was left blank, refactored code where a key constant was removed/renamed to empty, or dynamic keys built from DB rows containing empty strings.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Billionmail/BillionMail@fc36c76c05 (2026-09-05). Data as JSON: /api/errors/04ccf95cb0b721cb. Report an issue: GitHub.