JuliusBrussee/caveman · error

tenantkey: safe month and request id are required

Error message

tenantkey: safe month and request id are required

What it means

MonthlySpendEvent builds an idempotency-marker key from month and requestID and requires both to pass safePart. If either is empty or contains unsafe characters, the library refuses to construct the key, since a bad segment could break key namespacing or idempotency guarantees.

Solutions

  1. Generate the request ID before constructing the key (uuid.NewString() or the gateway request ID) and ensure month is formatted via t.Format("2006-01").
  2. Validate both inputs at the handler boundary: non-empty, trimmed, matching ^\d{4}-\d{2}$ for month and a safe ID pattern for requestID.
  3. When requestID comes from a header (e.g. X-Request-Id), require it explicitly and reject the request when missing instead of passing "" through.

Example fix

// before
key, err := tenantkey.MonthlySpendEvent(org, proj, month, r.Header.Get("X-Request-Id")) // may be ""
// after
month = strings.TrimSpace(month)
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
    requestID = uuid.NewString()
}
if !safe(month) || !safe(requestID) {
    return errors.New("month and request id must be non-empty and safe")
}
key, err := tenantkey.MonthlySpendEvent(org, proj, month, requestID)
Defensive patterns

Strategy: validation

Validate before calling

var idRe = regexp.MustCompile(`^[A-Za-z0-9_-]{1,128}$`)
func validSpendEventArgs(month, requestID string) bool {
    return regexp.MustCompile(`^\d{4}-\d{2}$`).MatchString(month) && idRe.MatchString(requestID)
}

Try / catch

key, err := tenantkey.MonthlySpendEvent(org, proj, month, requestID)
if err != nil && strings.Contains(err.Error(), "safe month and request id are required") {
    return fmt.Errorf("month %q / request id %q must be non-empty safe tokens", month, requestID)
}

Prevention

When it happens

Trigger: Calling tenantkey.MonthlySpendEvent(orgID, projectID, month, requestID) with month = "", requestID = "", or either containing unsafe characters (slashes, colons, spaces) — e.g. an ungenerated request ID or a month not normalized to "2026-09" form.

Common situations: Retry handler called before requestID was assigned (empty string); request ID taken from a header that was absent; month parsed with slashes; both fields passed through unvalidated from an HTTP request body.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/86e61d0d388ad0d1. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/tenantkey/tenantkey.go:38

func MonthlySpend(organizationID, projectID, month string) (string, error) {
	prefix, err := projectPrefix("spend", organizationID, projectID)
	if err != nil {
		return "", err
	}
	if !safePart(month) {
		return "", errors.New("tenantkey: safe month is required")
	}
	return prefix + ":" + month, nil
}

// MonthlySpendEvent returns idempotency-marker key for one spend event.
func MonthlySpendEvent(organizationID, projectID, month, requestID string) (string, error) {
	prefix, err := projectPrefix("spend-event", organizationID, projectID)
	if err != nil {
		return "", err
	}
	if !safePart(month) || !safePart(requestID) {
		return "", errors.New("tenantkey: safe month and request id are required")
	}
	return prefix + ":" + month + ":" + requestID, nil
}

func projectPrefix(namespace, organizationID, projectID string) (string, error) {
	if !safePart(organizationID) || !safePart(projectID) {
		return "", errors.New("tenantkey: safe organization and project scope are required")
	}
	return "cave:" + namespace + ":" + organizationID + ":" + projectID, nil
}

func safePart(value string) bool {
	value = strings.TrimSpace(value)
	return value != "" && !strings.ContainsAny(value, ":\r\n\x00")
}

View on GitHub (pinned to 3ee70a1026)