siyuan-note/siyuan · error

invalid sort position

Error message

invalid sort position [%s]

What it means

createDocByMd validates the optional sortPosition argument when a sortTargetID is supplied: it must be exactly "before" or "after". Any other string (including case variants or empty-with-target mismatch) returns this invalid sort position error and the document is not created.

Solutions

  1. Pass sortPosition as exactly "before" or "after" when sortTargetID is provided
  2. Omit sortPosition entirely when no positional placement is intended
  3. Validate/sanitize the position value in the calling UI before invoking the API
  4. Use lowercase ASCII keywords; do not localize the position strings

Example fix

// before
arg["sortPosition"] = "Before"
// after
arg["sortPosition"] = "before"
Defensive patterns

Strategy: validation

Validate before calling

function normalizeSortPosition(pos) {
  if (pos == null || pos === "") return null; // no positional placement
  const p = String(pos).toLowerCase();
  if (p !== "before" && p !== "after") throw new Error("sortPosition must be 'before' or 'after'");
  return p;
}

Type guard

function isValidSortPosition(v) {
  return v === "before" || v === "after";
}

Try / catch

try {
  await createDocWithMd(args);
} catch (e) {
  if (/invalid sort position/.test(e.message)) {
    delete args.sortPosition; delete args.sortTargetID; // retry with default placement
    return createDocWithMd(args);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling CreateDocByMd/CreateDocByMdSync with arg sortTargetID set and sortPosition set to a value other than "before"/"after" (e.g. "Before", "top", "end", or an arbitrary string).

Common situations: Third-party scripts and plugins guessing the position vocabulary; UI code passing a translated label instead of the internal keyword; API clients upgrading and inventing new position 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/ed11dbb9f17f518f. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/file.go:1301

// CreateDocByMdSync 同步创建 Markdown 文档并返回事务落盘错误,供成功后需要清理源数据的调用方使用。
func CreateDocByMdSync(boxID, p, title, md string, sorts []string, arg map[string]any) (tree *parse.Tree, err error) {
	return createDocByMd(boxID, p, title, md, sorts, arg, true)
}

func createDocByMd(boxID, p, title, md string, sorts []string, arg map[string]any, syncWrite bool) (tree *parse.Tree, err error) {
	createDocLock.Lock()
	defer createDocLock.Unlock()

	box, err := getOpenedBox(boxID)
	if nil != err {
		return
	}
	sortTargetID, _ := arg["sortTargetID"].(string)
	sortPosition, _ := arg["sortPosition"].(string)
	if "" != sortTargetID {
		if "before" != sortPosition && "after" != sortPosition {
			return nil, fmt.Errorf("invalid sort position [%s]", sortPosition)
		}
		target := treenode.GetBlockTree(sortTargetID)
		if !isSortableDocument(target) || target.BoxID != boxID || path.Dir(target.Path) != path.Dir(p) {
			return nil, fmt.Errorf("sort target document [%s] is not a sibling of the new document", sortTargetID)
		}
	} else if "" != sortPosition {
		return nil, errors.New("sort target ID is required when sort position is specified")
	}

	luteEngine := util.NewLute()
	luteEngine.SetHTMLTag2TextMark(true)
	dom := luteEngine.Md2BlockDOM(md, false)
	tree, err = createDoc0(box.ID, p, title, dom, false, syncWrite)
	if err != nil {
		return
	}

	FlushTxQueue()

View on GitHub (pinned to 9f775e8a12)