kataras/iris · error

iris: rewrite: decode:

Error message

iris: rewrite: decode: 

What it means

After opening the rules file, rewrite.LoadOptions decodes it with the yaml or json decoder chosen by extension; a decode error is re-panicked prefixed with 'iris: rewrite: decode: '. The file was found and the extension was valid, but its contents do not parse as the corresponding format or do not fit the Options structure.

Source

Thrown at middleware/rewrite/rewrite.go:60

	}

	f, err := os.Open(filename)
	if err != nil {
		panic("iris: rewrite: " + err.Error())
	}
	defer f.Close()

	switch ext {
	case ".yaml", ".yml":
		err = yaml.NewDecoder(f).Decode(&opts)
	case ".json":
		err = json.NewDecoder(f).Decode(&opts)
	default:
		panic("iris: rewrite: unexpected file extension: " + filename)
	}

	if err != nil {
		panic("iris: rewrite: decode: " + err.Error())
	}

	return
}

// Rewrite is a struct that represents a rewrite engine for Iris web framework.
// It contains a slice of redirect rules, an options struct, a logger, and a domain validator function.
// It provides methods to create, configure, and apply rewrite rules to HTTP requests and responses.
//
// Navigate through _examples/routing/rewrite for more.
type Engine struct {
	redirects []*redirectMatch
	options   Options

	logger          *golog.Logger
	domainValidator func(string) bool
}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Validate the file with a YAML/JSON parser and fix the reported syntax error (line/column is in the panic message).
  2. Check that the document structure matches rewrite.Options (primary ROOT redirection plus list of redirect match rules).
  3. Test parsing in a unit test with yaml.Unmarshal/json.Unmarshal on the same file so failures surface with full error detail.

Example fix

// before (redirects.yml)
redirects:
	- /old /new   # tab indentation -> decode error
// after (redirects.yml)
redirects:
  - /old /new
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(rulesPath)
if err != nil { return err }
switch strings.ToLower(filepath.Ext(rulesPath)) {
case ".yaml", ".yml":
    var v any
    if err := yaml.Unmarshal(data, &v); err != nil {
        return fmt.Errorf("invalid rewrite yaml: %w", err)
    }
case ".json":
    if !json.Valid(data) {
        return errors.New("invalid rewrite json")
    }
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        log.Fatalf("rewrite rules decode failed: %v", r)
    }
}()
engine := rewrite.Load(rulesPath)

Prevention

When it happens

Trigger: Loading a .yml file with YAML syntax errors (bad indentation, tabs), or a .json file with trailing commas/comments, or content whose types don't match rewrite.Options fields (e.g. a string where a list of RedirectMatch objects is expected).

Common situations: Hand-edited YAML using tabs instead of spaces; JSON file exported with comments; redirect rules defined in a shape the Options struct doesn't recognize; merge artifacts (<<<<<<< markers) left in the file.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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