inancgumus/learngo · error

incorrect %s: %q

Error message

incorrect %s: %q

What it means

field.atoi converts a numeric field with strconv.Atoi and rejects values that are negative or non-numeric, storing the error on the field struct as 'incorrect %s: %q' where %s is the field name ('visits' or 'time spent') and %q the offending value. The caller is expected to check f.err after parsing all fields.

Source

Thrown at logparser/testing/report/result.go:58

	fields := strings.Fields(line)
	if len(fields) != 3 {
		return r, fmt.Errorf("missing fields: %v", fields)
	}

	f := new(field)
	r.Domain = fields[0]
	r.Visits = f.atoi("visits", fields[1])
	r.TimeSpent = f.atoi("time spent", fields[2])
	return r, f.err
}

// field helps for field parsing
type field struct{ err error }

func (f *field) atoi(name, val string) int {
	n, err := strconv.Atoi(val)
	if n < 0 || err != nil {
		f.err = fmt.Errorf("incorrect %s: %q", name, val)
	}
	return n
}

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Correct the offending value in the log line
  2. Pre-validate numeric fields with a regex like ^\d+$ before atoi
  3. Strip units/separators (e.g. remove commas, trailing time units) before conversion
  4. Check f.err immediately after parse so the right field/value is reported to the user

Example fix

// before
n, err := strconv.Atoi(val)
if n < 0 || err != nil {
    f.err = fmt.Errorf("incorrect %s: %q", name, val)
}
// after
n, err := strconv.Atoi(strings.TrimSuffix(val, "s"))
if err != nil || n < 0 {
    f.err = fmt.Errorf("incorrect %s: %q (must be a non-negative integer)", name, val)
}
Defensive patterns

Strategy: validation

Validate before calling

var numRe = regexp.MustCompile(`^\d+$`)
func isNonNegativeInt(s string) bool { return numRe.MatchString(s) }
// check fields[1] and fields[2] with isNonNegativeInt before parse

Type guard

func isCount(s string) bool {
    n, err := strconv.Atoi(s)
    return err == nil && n >= 0
}

Try / catch

r, err := parse(line)
if err != nil {
    if f.err != nil { // field.atoi stored the failure
        log.Printf("bad numeric field in %q: %v", line, f.err)
        continue
    }
    return err
}

Prevention

When it happens

Trigger: A log line whose visits or time-spent column is negative (-5) or not a plain integer (e.g. 'abc', '1.5', '12,000', '0x10').

Common situations: Units accidentally included ('30s', '45min'), thousands separators, signed values from upstream systems, float durations, or corrupted log data.

Related errors


AI-assisted analysis of inancgumus/learngo@3c475a78e5 (2026-09-02). Data as JSON: /api/errors/2278599ccaea2171. Report an issue: GitHub.