siyuan-note/siyuan · error · ErrInvalidAttributeViewContextFilter

%w: %v

Error message

%w: %v

What it means

ParseAttributeViewContextFilter wraps any JSON decoding failure of a context filter into ErrInvalidAttributeViewContextFilter with the underlying decoder message appended via %w/%v. Decoding uses DisallowUnknownFields, so unknown properties are also rejected.

Source

Thrown at kernel/av/context_filter.go:66

// FilterContext 保存一次数据库块渲染所需的上下文筛选值,不参与持久化。
type FilterContext struct {
	KeyID                  string
	CurrentDocumentItemIDs []string
}

// ParseAttributeViewContextFilter 从数据库块 IAL 中解析上下文筛选配置。
func ParseAttributeViewContextFilter(data string) (ret *AttributeViewContextFilter, err error) {
	data = strings.TrimSpace(data)
	if "" == data {
		return
	}

	decoder := json.NewDecoder(bytes.NewBufferString(data))
	decoder.DisallowUnknownFields()
	ret = &AttributeViewContextFilter{}
	if err = decoder.Decode(ret); nil != err {
		err = fmt.Errorf("%w: %v", ErrInvalidAttributeViewContextFilter, err)
		ret = nil
		return
	}
	if err = ensureAttributeViewContextFilterJSONEOF(decoder); nil != err {
		ret = nil
		return
	}
	if AttributeViewContextFilterSpec != ret.Spec || "" == strings.TrimSpace(ret.KeyID) {
		err = ErrInvalidAttributeViewContextFilter
		ret = nil
	}
	return
}

func ensureAttributeViewContextFilterJSONEOF(decoder *json.Decoder) error {
	var trailing any
	err := decoder.Decode(&trailing)
	if errors.Is(err, io.EOF) {

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Fix the JSON syntax and ensure spec is a number and keyID a string
  2. Remove any unknown properties, since DisallowUnknownFields makes them fatal
  3. Read the wrapped %v detail in the error message to identify the exact decode failure

Example fix

// before
{"spec": "1", "keyID": "k1", "extra": true}
// after
{"spec": 1, "keyID": "k1"}
Defensive patterns

Strategy: try-catch

Validate before calling

function canParseFilterJSON(s) { try { const o = JSON.parse(s); return o && typeof o === 'object' && !('extra' in o); } catch { return false; } }

Try / catch

filter, err := ParseAttributeViewContextFilter(data)
if err != nil {
    if errors.Is(err, av.ErrInvalidAttributeViewContextFilter) { logInvalidFilter(data, err) } else { return err }
}

Prevention

When it happens

Trigger: Calling ParseAttributeViewContextFilter with malformed JSON, wrong JSON types (e.g. spec as string), or extra unknown fields in the payload.

Common situations: Hand-edited filter JSON in document attributes; newer format parsed by an older kernel (or vice versa); trailing garbage after the JSON object.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/9f62cfc7b60ecbc9. Report an issue: GitHub.