siyuan-note/siyuan · error

invalid view state key

Error message

invalid view state key

What it means

validateViewStateKey rejects a view state key that is empty/whitespace or longer than 1024 characters. GetViewState, PatchViewState, and RemoveViewState all call it first, so any view-state operation with a malformed key fails with this error.

Solutions

  1. Ensure the key is a non-empty, trimmed identifier before calling (e.g. a document/view ID)
  2. Truncate or hash over-long composite keys to keep them under 1024 characters
  3. Validate the key client-side and skip the API call when it is blank

Example fix

// before
GetViewState(strings.Join(longParts, "/")) // > 1024 chars
// after
k := strings.Join(longParts, "/")
if len(k) > 1024 { k = k[:512] + "__" + hash(k) }
GetViewState(k)
Defensive patterns

Strategy: validation

Validate before calling

func validViewKey(k string) bool {
    k = strings.TrimSpace(k)
    return k != "" && len(k) <= 1024
}
if !validViewKey(key) { return errors.New("invalid view state key") }

Try / catch

if _, err := GetViewState(key); err != nil && err.Error() == "invalid view state key" {
    // fix or regenerate the key before retrying
}

Prevention

When it happens

Trigger: Calling GetViewState/PatchViewState/RemoveViewState with "" or a blank string, or with a key exceeding 1024 chars (e.g. an entire serialized object used as the key, or concatenated IDs).

Common situations: Frontend code building keys by joining IDs without bounds; passing an uninitialized variable as the key; forgetting the key argument in an API request so the empty string is validated.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/view_state.go:213

					return keys[i] < keys[j]
				}
				return left.Updated < right.Updated
			})
			for i, key := range keys {
				views[key].Updated = int64(i + 1)
			}
			return int64(len(keys) + 1)
		}
		if ret <= state.Updated {
			ret = state.Updated + 1
		}
	}
	return ret
}

func validateViewStateKey(key string) error {
	if "" == strings.TrimSpace(key) || 1024 < len(key) {
		return errors.New("invalid view state key")
	}
	return nil
}

func validateViewStateDataKey(key string) error {
	if "" == strings.TrimSpace(key) || 2048 < len(key) {
		return errors.New("invalid view state data key")
	}
	return nil
}

func pruneViewStates(views map[string]*ViewState) {
	if len(views) <= maxViewStateCount {
		return
	}

	keys := make([]string, 0, len(views))
	for key := range views {

View on GitHub (pinned to 9f775e8a12)