siyuan-note/siyuan · error

element [ ]

Error message

element [%d]: %w

What it means

When decoding a JSON array into a slice-typed contract field, decodeRequestValue decodes each element recursively and wraps any per-element failure as "element [%d]: %w" with the failing index. The error indicates which element of the array failed to decode, with the underlying cause appended via %w. It is positional diagnostics over the apicontract strict decoder.

Solutions

  1. Inspect the reported index and fix that element's type to match the declared element type
  2. Validate every array element is a string/valid JSON of the right type before sending
  3. Filter out null/undefined entries from the array client-side
  4. Check the endpoint contract in kernel/apicontract/ for the exact element type

Example fix

// before
srcs: ["a.sy", null, "c.sy"]
// after
srcs: ["a.sy", "c.sy"].filter(Boolean)
Defensive patterns

Strategy: validation

Validate before calling

function validateArray(arr, check) {
  if (!Array.isArray(arr)) return;
  arr.forEach((el, i) => { if (!check(el)) throw new Error(`element [${i}] invalid`); });
}
validateArray(payload.srcs, (s) => typeof s === "string");

Type guard

const allOfType = (arr, t) => Array.isArray(arr) && arr.every((x) => typeof x === t);

Try / catch

try { await fetchPost(url, payload); } catch (e) { const m = String(e).match(/element \[(\d+)]/); if (m) console.error("Bad array element at", m[1], payload.ids[m[1]]); }

Prevention

When it happens

Trigger: Calling any kernel API with an array field (e.g. srcs, ids, av column lists) where at least one element does not match the slice's element type — e.g. ["a", null, 3] for a []string field, or an object inside a string array.

Common situations: Bulk operations over lists of block/notebook IDs where one entry is null or a number; JavaScript arrays with mixed types; payloads serialized from Set/Map objects producing unexpected element shapes.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at kernel/apicontract/decode.go:192

	switch value.Kind() {
	case reflect.Pointer:
		value.Set(reflect.New(value.Type().Elem()))
		return decodeRequestValue(raw, value.Elem())
	case reflect.Struct:
		var fields map[string]json.RawMessage
		if err := json.Unmarshal(raw, &fields); err != nil {
			return err
		}
		return decodeRequestFields(value, fields)
	case reflect.Slice:
		var entries []json.RawMessage
		if err := json.Unmarshal(raw, &entries); err != nil {
			return err
		}
		value.Set(reflect.MakeSlice(value.Type(), len(entries), len(entries)))
		for i, entry := range entries {
			if err := decodeRequestValue(entry, value.Index(i)); err != nil {
				return fmt.Errorf("element [%d]: %w", i, err)
			}
		}
		return nil
	case reflect.Map:
		var entries map[string]json.RawMessage
		if err := json.Unmarshal(raw, &entries); err != nil {
			return err
		}
		value.Set(reflect.MakeMapWithSize(value.Type(), len(entries)))
		for key, entry := range entries {
			child := reflect.New(value.Type().Elem()).Elem()
			if err := decodeRequestValue(entry, child); err != nil {
				return fmt.Errorf("entry [%s]: %w", key, err)
			}
			value.SetMapIndex(reflect.ValueOf(key).Convert(value.Type().Key()), child)
		}
		return nil
	default:

View on GitHub (pinned to 9f775e8a12)