vitessio/vitess · error
invalid truncation location: %d
Error message
invalid truncation location: %d
What it means
TruncateText in go/textutil/strings.go takes an explicit truncation location; any value other than TruncationLocationStart, TruncationLocationMiddle, or TruncationLocationEnd hits the default branch and returns this error. It enforces the enum contract instead of guessing a behavior.
Source
Thrown at go/textutil/strings.go:164
// length using the provided truncation indicator in place of the truncated text
// in the specified location of the original string.
func TruncateText(text string, limit int, location TruncationLocation, indicator string) (string, error) {
ol := len(text)
if ol <= limit {
return text, nil
}
if len(indicator)+2 >= limit {
return "", errors.New("the truncation indicator is too long for the provided text")
}
switch location {
case TruncationLocationMiddle:
prefix := (limit / 2) - len(indicator)
suffix := (ol - (prefix + len(indicator))) + 1
return fmt.Sprintf("%s%s%s", text[:prefix], indicator, text[suffix:]), nil
case TruncationLocationEnd:
return text[:limit-(len(indicator)+1)] + indicator, nil
default:
return "", fmt.Errorf("invalid truncation location: %d", location)
}
}
View on GitHub (pinned to 01a25a7d17)
Solutions
- Pass one of the exported constants: TruncationLocationStart, TruncationLocationMiddle, or TruncationLocationEnd
- Replace magic-number locations in call sites with the constants
- Clamp or map dynamic location values to a valid constant before calling
Example fix
// before textutil.TruncateText(s, 80, "...", 3) // after textutil.TruncateText(s, 80, "...", textutil.TruncationLocationMiddle)
Defensive patterns
Strategy: validation
Validate before calling
func validLocation(l textutil.TruncationLocation) bool {
switch l {
case textutil.TruncationLocationStart, textutil.TruncationLocationMiddle, textutil.TruncationLocationEnd:
return true
}
return false
} Try / catch
out, err := textutil.TruncateText(s, 80, "...", loc)
if err != nil {
out, _ = textutil.TruncateText(s, 80, "...", textutil.TruncationLocationEnd)
} Prevention
- Always use the exported TruncationLocation* constants, never raw ints
- Add a switch exhaustiveness check (golangci default-case linter) over the enum
- Clamp externally sourced location values into the valid range before calling
When it happens
Trigger: Calling textutil.TruncateText(text, limit, indicator, location) with a location int that is not one of the defined TruncationLocation constants (e.g. 3, -1, or a value cast from elsewhere).
Common situations: Switching API versions where a new/renamed location constant is missed; computing the location dynamically and going out of range; copying call sites with magic numbers.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- ReadFile cannot be called on read-write backup
- AddFile cannot be called on read-only backup
- EndBackup cannot be called on read-only backup
- AbortBackup cannot be called on read-only backup
- ReadFile cannot be called on read-write backup
AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01).
Data as JSON: /api/errors/4d17b83bb454c67b.
Report an issue: GitHub.