gofr-dev/gofr · error

failed to parse YAML config file %s: %w

Error message

failed to parse YAML config file %s: %w

What it means

LoadPermissions failed to parse the RBAC config file as YAML. yaml.Unmarshal rejected the file contents (or the wrapper surfaced a prior read/format failure), so the RBAC permission set could not be built and EnableRBAC aborts. The path is included so you can locate the offending file.

Source

Thrown at pkg/gofr/rbac/config.go:147

// LoadPermissions loads RBAC configuration from a JSON or YAML file.
// The file format is automatically detected based on the file extension.
// Supported formats: .json, .yaml, .yml.
// Dependencies (logger, metrics, tracer) are optional and can be set after loading.
func LoadPermissions(path string, logger datasource.Logger, metrics container.Metrics, tracer trace.Tracer) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("failed to read RBAC config file %s: %w", path, err)
	}

	var config Config

	// Detect file format by extension
	ext := strings.ToLower(filepath.Ext(path))
	switch ext {
	case ".yaml", ".yml":
		if err := yaml.Unmarshal(data, &config); err != nil {
			return nil, fmt.Errorf("failed to parse YAML config file %s: %w", path, err)
		}
	case ".json", "":
		if err := json.Unmarshal(data, &config); err != nil {
			return nil, fmt.Errorf("failed to parse JSON config file %s: %w", path, err)
		}
	default:
		return nil, fmt.Errorf("unsupported config file format: %s (supported: .json, .yaml, .yml): %w", ext, errUnsupportedFormat)
	}

	// Set dependencies
	config.Logger = logger
	config.Metrics = metrics
	config.Tracer = tracer

	// Initialize mux router for pattern matching
	// Use StrictSlash(false) to match the application router's behavior
	config.muxRouter = mux.NewRouter().StrictSlash(false)

View on GitHub (pinned to 187eb24962)

Solutions

  1. Validate the YAML file with a linter/parser (yamllint or `go run gopkg.in/yaml.v3` unmarshal) and fix syntax at the reported line.
  2. Confirm the file at the path in the message exists and is non-empty; check for failed template rendering or truncated writes.
  3. If the content is JSON, rename the file to .json (or remove the extension) so the JSON branch parses it.
  4. Ensure the structs match the YAML shape (field names/tags) if unmarshalling reports type errors wrapped in this message.

Example fix

// before (rbac.yaml)
roles:
	admin:
  - users:read
// after (spaces, not tabs)
roles:
  admin:
    - users:read
Defensive patterns

Strategy: try-catch

Validate before calling

data, err := os.ReadFile(path)
if err != nil { return err }
if strings.ToLower(filepath.Ext(path)) == ".yaml" || strings.ToLower(filepath.Ext(path)) == ".yml" {
    var probe map[string]any
    if err := yaml.Unmarshal(data, &probe); err != nil {
        return fmt.Errorf("rbac yaml %s invalid: %w", path, err)
    }
}

Try / catch

config, err := rbac.LoadPermissions(path, logger, metrics, tracer)
if err != nil {
    var perr *yaml.TypeError
    if errors.As(err, &perr) || strings.Contains(err.Error(), "failed to parse YAML") {
        logger.Fatalf("fix rbac yaml %v: %v", path, err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling EnableRBAC/LoadPermissions with a .yaml or .yml config file whose contents are not valid YAML: bad indentation, tabs instead of spaces, unquoted special characters, duplicate keys, or the file extension says YAML but the content is JSON/other.

Common situations: Hand-edited rbac.yaml with a broken indent; template rendering produced invalid YAML; a CI step wrote an empty or partial file; someone renamed a .json config to .yaml without converting it.

Understand the failure class

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/a4e009c459a69712. Report an issue: GitHub.