hyperledger/fabric · error

error unmarshalling YAML

Error message

error unmarshalling YAML

What it means

GetConfig reads a connection profile and unmarshals it into the NetworkConfig struct using yaml.Unmarshal. This error is wrapped around any YAML parse failure, meaning the file was readable but is not valid YAML or does not match the expected NetworkConfig schema.

Source

Thrown at internal/peer/common/networkconfig.go:172

}

// GetConfig unmarshals the provided connection profile into a network
// configuration struct
func GetConfig(fileName string) (*NetworkConfig, error) {
	if fileName == "" {
		return nil, errors.New("filename cannot be empty")
	}

	data, err := os.ReadFile(fileName)
	if err != nil {
		return nil, errors.Wrap(err, "error reading connection profile")
	}

	configData := string(data)
	config := &NetworkConfig{}
	err = yaml.Unmarshal([]byte(configData), &config)
	if err != nil {
		return nil, errors.Wrap(err, "error unmarshalling YAML")
	}

	return config, nil
}

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Validate the YAML with a linter or `yamllint <file>` to find syntax errors
  2. Convert tabs to spaces — YAML forbids tab indentation
  3. Compare against a known-good sample connection profile from test-network fixtures
  4. Ensure required NetworkConfig fields (organizations, peers, certificateAuthorities) exist with correct nesting

Example fix

// before (invalid: tab indentation)
// channels:\n// \tmychannel: ... (tab char in file)
// after
// channels:
//   mychannel: ...  (spaces only)
Defensive patterns

Strategy: validation

Validate before calling

func validYAML(path string) error {
    data, err := os.ReadFile(path)
    if err != nil {
        return err
    }
    var v map[string]interface{}
    if err := yaml.Unmarshal(data, &v); err != nil {
        return fmt.Errorf("invalid YAML in %s: %w", path, err)
    }
    return nil
}

Try / catch

config, err := common.GetConfig(profilePath)
if err != nil {
    if strings.Contains(err.Error(), "error unmarshalling YAML") {
        log.Fatalf("connection profile %q is not valid YAML: %v", profilePath, err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling GetConfig on a file containing invalid YAML syntax (bad indentation, tabs, stray characters), a JSON profile without proper structure, or YAML whose top-level shape does not match NetworkConfig fields.

Common situations: Hand-edited connection profiles with indentation mistakes; saving a JSON connection profile and passing it to a YAML parser path; copy-pasting YAML that introduced tabs; schema mismatch after upgrading Fabric version where profile fields changed.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/3c720bb9aa763d39. Report an issue: GitHub.