siyuan-note/siyuan · error

invalid heading fold scope

Error message

invalid heading fold scope

What it means

GetHeadingFoldTransaction folds (or unfolds) a heading and computes the affected headings according to a scope argument. Only certain scope values (e.g. "children", "siblings", and the direct/recursive variants shown above) are recognized; any other string reaches the default branch and returns "invalid heading fold scope".

Solutions

  1. Use one of the supported scope values exactly (check the API handler for the accepted set, e.g. "children", "siblings")
  2. Add a client-side allowlist of valid scope values before issuing the call
  3. Catch the error and surface which scope string was rejected so the config can be fixed

Example fix

// before
transaction, err := model.GetHeadingFoldTransaction(id, scope, true) // scope from config
// after
var validScopes = map[string]bool{"children": true, "siblings": true}
if !validScopes[scope] {
    return fmt.Errorf("unsupported fold scope %q", scope)
}
transaction, err := model.GetHeadingFoldTransaction(id, scope, true)
Defensive patterns

Strategy: validation

Validate before calling

var validFoldScopes = map[string]bool{"children": true, "siblings": true}
if !validFoldScopes[scope] {
    return fmt.Errorf("fold scope %q not supported", scope)
}
transaction, err := model.GetHeadingFoldTransaction(headingID, scope, isFold)

Type guard

func validScope(s string) bool {
    switch s {
    case "children", "siblings":
        return true
    }
    return false
}

Try / catch

transaction, err := model.GetHeadingFoldTransaction(id, scope, fold)
if err != nil && strings.Contains(err.Error(), "invalid heading fold scope") {
    return fmt.Errorf("scope %q rejected; check API docs for allowed values", scope)
}

Prevention

When it happens

Trigger: Calling GetHeadingFoldTransaction(headingID, scope, isFold) with a scope value outside the enumerated set — a typo like "child" or "subtree", an empty string, or a scope name invented by a plugin.

Common situations: Frontend/plugin passes a user-configurable fold-scope option that doesn't match the kernel's enum; API consumers guessing scope names instead of reading the handler's allowed values.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/608a4ef29d00df46. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/heading.go:61

	heading := treenode.GetNodeInTree(tree, id)
	transaction = &Transaction{}
	if nil == heading || ast.NodeHeading != heading.Type {
		return
	}

	var headings []*ast.Node
	switch scope {
	case "children":
		if treenode.IsSelfFolded(heading) {
			headings = []*ast.Node{heading}
		} else {
			headings = treenode.HeadingDirectChildren(heading)
		}
	case "siblings":
		headings = treenode.HeadingSiblings(heading)
	default:
		err = errors.New("invalid heading fold scope")
		return
	}
	transaction = buildHeadingFoldTransaction(headings)
	return
}

func buildHeadingFoldTransaction(headings []*ast.Node) (transaction *Transaction) {
	transaction = &Transaction{}
	foldAll := false
	for _, heading := range headings {
		if !treenode.IsSelfFolded(heading) {
			foldAll = true
			break
		}
	}

	for i := len(headings) - 1; 0 <= i; i-- {
		heading := headings[i]

View on GitHub (pinned to 9f775e8a12)