gofr-dev/gofr · error
endpoint[%d]: invalid mux pattern: %w
Error message
endpoint[%d]: invalid mux pattern: %w
What it means
validateEndpointPath detected the path uses gorilla/mux variable syntax ({var}) and validateMuxPattern found the pattern syntactically invalid. RBAC paths must be valid mux patterns so routing and permission matching agree.
Source
Thrown at pkg/gofr/rbac/config.go:222
// Reject wildcard patterns
if err := c.checkWildcardPattern(path, index); err != nil {
return err
}
// Reject old regex patterns
if err := c.checkRegexPattern(path, index); err != nil {
return err
}
// Reject regex indicators outside of variable constraints
if err := c.checkRegexIndicators(path, index); err != nil {
return err
}
// Validate mux pattern syntax if it contains variables
if isMuxPattern(path) {
if err := validateMuxPattern(path); err != nil {
return fmt.Errorf("endpoint[%d]: invalid mux pattern: %w", index, err)
}
}
return nil
}
// checkWildcardPattern checks if path contains wildcard pattern.
func (*Config) checkWildcardPattern(path string, index int) error {
if strings.Contains(path, "/*") {
return fmt.Errorf("endpoint[%d]: %w: %s. Examples: /api/{resource} for single-level or /api/{path:.*} for multi-level",
index, errWildcardPatternNotSupported, path)
}
return nil
}
// checkRegexPattern checks if path contains old regex pattern.
func (*Config) checkRegexPattern(path string, index int) error {View on GitHub (pinned to 187eb24962)
Solutions
- Balance all braces and give every variable a name: /api/{id}.
- If using a constraint, keep the colon and a valid regex: /api/users/{id:[0-9]+}.
- Replace multi-level needs with /api/{path:.*} instead of several broken variables.
- Test the pattern against your mux router to confirm it compiles/matches.
Example fix
// before
path: "/api/users/{id"
// after
path: "/api/users/{id:[0-9]+}" Defensive patterns
Strategy: validation
Validate before calling
func validMuxPath(p string) bool {
depth := 0
for _, r := range p {
switch r {
case '{':
depth++
case '}':
depth--
if depth < 0 { return false }
}
}
if depth != 0 { return false }
for _, m := range muxVarRe.FindAllStringSubmatch(p, -1) {
if m[1] == "" { return false } // empty variable name
}
return true
}
var muxVarRe = regexp.MustCompile(`\{([^:}]*)(:[^}]*)?\}`) Try / catch
for i, ep := range endpoints {
if !validMuxPath(ep.Path) { return fmt.Errorf("endpoint[%d]: bad mux pattern %q", i, ep.Path) }
} Prevention
- Test each pattern against the real mux router in unit tests.
- Keep brace count balanced; never leave an unclosed {var.
- Always name variables; provide valid regex constraints after ':'.
- Only copy patterns from mux documentation, not regex routers.
When it happens
Trigger: An endpoint path like /api/{id or /api/{id:} or /api/users[0]} — malformed braces, empty variable names, or invalid constraint expressions — passed to EnableRBAC/LoadPermissions.
Common situations: Hand-writing mux patterns with unbalanced braces; copying patterns from regex-style routers; typos in variable constraints like {id:[a-z instead of {id:[a-z]+}.
Related errors
- endpoint[%d]: %w: %s. Examples: /api/{resource} for single-l
- endpoint[%d]: %w: %s. Example: /api/users/{id:[0-9]+} instea
- endpoint[%d]: %w: %s. Example: /api/users/{id:[0-9]+}
- invalid RBAC config: %w
- endpoint[%d]: %w: %s
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/0fac0f06da77da14.
Report an issue: GitHub.