kataras/iris · error

empty regex expression

Error message

empty regex expression

What it means

macro.Regexp compiles a macro constraint expression into a MatchString func and returns 'empty regex expression' if expr is an empty string. MustRegexp calls Regexp and panics on error, so this guards against configuring a regexp-based param type with no expression.

Source

Thrown at macro/macro.go:58

			}

			if typOut.NumIn() == 1 && typOut.NumOut() == 1 { // if it's a type of func(paramValue [int,string...]) bool, used for param funcs.
				return typOut.Out(0).Kind() == reflect.Bool
			}
		}
	}

	return false
}

// Regexp accepts a regexp "expr" expression
// and returns its MatchString.
// The regexp is compiled before return.
//
// Returns a not-nil error on regexp compile failure.
func Regexp(expr string) (func(string) bool, error) {
	if expr == "" {
		return nil, fmt.Errorf("empty regex expression")
	}

	// add the last $ if missing (and not wildcard(?))
	if i := expr[len(expr)-1]; i != '$' && i != '*' {
		expr += "$"
	}

	r, err := regexp.Compile(expr)
	if err != nil {
		return nil, err
	}

	return r.MatchString, nil
}

// MustRegexp same as Regexp
// but it panics on the "expr" parse failure.
func MustRegexp(expr string) func(string) bool {

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Supply a valid non-empty regexp expression to Regexp/MustRegexp.
  2. If the expression comes from configuration, validate it is non-empty (and compiles) before calling Regexp.
  3. Provide a sensible default expression when the configured value is empty.

Example fix

// before
eval := macro.MustRegexp(cfg.Pattern) // panics if empty
// after
if cfg.Pattern == "" {
    cfg.Pattern = "^[a-zA-Z0-9_-]+$"
}
eval := macro.MustRegexp(cfg.Pattern)
Defensive patterns

Strategy: validation

Validate before calling

if expr == "" {
    return errors.New("macro regexp expression must be configured")
}
if _, err := regexp.Compile(expr); err != nil {
    return fmt.Errorf("invalid macro regexp %q: %w", expr, err)
}

Type guard

func validRegexExpr(expr string) bool {
    if expr == "" { return false }
    _, err := regexp.Compile(expr)
    return err == nil
}

Try / catch

defer func() {
    if r := recover(); r != nil { // MustRegexp panics on error
        log.Fatalf("macro regexp setup failed: %v", r)
    }
}()

Prevention

When it happens

Trigger: Calling macro.Regexp("") directly, or defining a custom macro/param type whose regexp constraint is an empty string (which would otherwise panic via MustRegexp).

Common situations: Building custom param types with a regexp pulled from config/env that is unset; template-generated macros where the expression variable is empty.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/eaae4731ac6b1949. Report an issue: GitHub.