siyuan-note/siyuan · error · Error

Invalid view state field

Error message

Invalid view state field

What it means

validateField constrains each view state field name to a non-empty string of at most 2048 characters. Fields are persisted as storage record keys, so empty or oversized names would break storage semantics. The error is thrown synchronously by has/get/set/patch before any network request.

Solutions

  1. Check field is a non-empty string of length <= 2048 before calling the API
  2. Use short, stable field names; store large values as the value, not the key
  3. Sanitize dynamic key parts (trim, cap length) at the component boundary
  4. Catch the error and fall back to a default field name

Example fix

// before
service.set(`${longPrefix}:${id}`, value); // > 2048 chars
// after
service.set(id.slice(0, 64), value);
Defensive patterns

Strategy: validation

Validate before calling

const safeField = (f) => (typeof f === "string" && f.length > 0 && f.length <= 2048) ? f : "default";
service.set(safeField(myField), value);

Type guard

const isValidField = (f) => typeof f === "string" && f.length > 0 && f.length <= 2048;

Try / catch

try { service.get(field); } catch (e) { if (e.message === "Invalid view state field") service.get("default"); }

Prevention

When it happens

Trigger: Calling service.has/get/set/patch (via validateField, app/src/util/viewState.ts:70) with field === "" or a field name longer than 2048 chars — e.g. building keys by concatenation where a segment was undefined ("undefined" is fine, but template gaps yield empty strings), or accidentally passing a serialized value instead of a name.

Common situations: Dynamic field names from user data or long IDs; joining path segments where one is empty; passing an object or JSON blob as the field argument by mistake.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at app/src/util/viewState.ts:70

        checkResponse(response, "get");
        return isViewStateData(response.data) ? response.data : {};
    },
    async patch(key, values, removeKeys) {
        const {fetchSyncPost} = await import("./fetch");
        const response = await fetchSyncPost("/api/storage/patchViewState", {key, values, removeKeys});
        checkResponse(response, "patch");
    },
};

const validateIdentityPart = (name: string, value: string) => {
    if (!value) {
        throw new Error(`View state ${name} must not be empty`);
    }
};

const validateField = (field: string) => {
    if (!field || field.length > 2048) {
        throw new Error("Invalid view state field");
    }
};

export const getViewStateKey = (identity: IViewStateIdentity) => {
    validateIdentityPart("scope", identity.scope);
    validateIdentityPart("surface", identity.surface);
    validateIdentityPart("hostID", identity.hostID);
    const key = [identity.scope, identity.surface, identity.hostID].map(encodeURIComponent).join(":");
    if (key.length > 1024) {
        throw new Error("View state key is too long");
    }
    return key;
};

export class ViewStateService {
    public readonly key: string;
    public readonly ready: Promise<void>;
    private readonly data: TViewStateData = {};

View on GitHub (pinned to 9f775e8a12)