nats-io/nats-server · error

failed to parse int from %s: %s

Error message

failed to parse int from %s: %s

What it means

mustParseInt converts a cron field token to a non-negative integer using strconv.Atoi; any token that is not a plain valid integer produces this error wrapping the underlying strconv error. Names are resolved earlier via parseIntOrName's name map, so this fires for non-numeric, non-name tokens.

Source

Thrown at server/cron.go:250

	}
	return getBits(start, end, step) | extra, nil
}

// parseIntOrName returns the (possibly-named) integer contained in expr.
func parseIntOrName(expr string, names map[string]uint) (uint, error) {
	if names != nil {
		if namedInt, ok := names[strings.ToLower(expr)]; ok {
			return namedInt, nil
		}
	}
	return mustParseInt(expr)
}

// mustParseInt parses the given expression as an int or returns an error.
func mustParseInt(expr string) (uint, error) {
	num, err := strconv.Atoi(expr)
	if err != nil {
		return 0, fmt.Errorf("failed to parse int from %s: %s", expr, err)
	}
	if num < 0 {
		return 0, fmt.Errorf("negative number (%d) not allowed: %s", num, expr)
	}
	return uint(num), nil
}

// getBits sets all bits in the range [min, max], modulo the given step size.
func getBits(min, max, step uint) uint64 {
	var bits uint64

	// If step is 1, use shifts.
	if step == 1 {
		return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min)
	}

	// Else, use a simple loop.
	for i := min; i <= max; i += step {

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Correct the token to a valid integer (e.g. "*/5" instead of "*/abc")
  2. Use numeric values instead of names not supported by the field's name map
  3. Remove stray characters/empty segments (double commas, trailing separators)

Example fix

// before (month field)
 expr := "jan-3"
// after
 expr := "1-3"
Defensive patterns

Strategy: validation

Validate before calling

func isIntToken(s string) bool {
    _, err := strconv.Atoi(s)
    return err == nil && s != ""
}

Try / catch

if _, err := parseCron(pattern, nil, ts); err != nil {
    return fmt.Errorf("schedule %q invalid: %w", pattern, err)
}

Prevention

When it happens

Trigger: A cron field containing invalid tokens like "*/abc", "1--3", "1o", empty segments (e.g. double commas "1,,3"), passed via getRange or parseIntOrName during parseCron.

Common situations: Typos in the schedule string; month/day names like "jan" or "mon" used in a field whose name map does not include them; stray whitespace collapsed into malformed tokens.

Understand the failure class

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/8312a57e0efd175c. Report an issue: GitHub.