{"record":{"id":"ed11dbb9f17f518f","repo":"siyuan-note/siyuan","slug":"invalid-sort-position-s","errorCode":null,"errorMessage":"invalid sort position [%s]","messagePattern":"invalid sort position \\[(.+?)\\]","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/file.go","lineNumber":1301,"sourceCode":"\n// CreateDocByMdSync 同步创建 Markdown 文档并返回事务落盘错误，供成功后需要清理源数据的调用方使用。\nfunc CreateDocByMdSync(boxID, p, title, md string, sorts []string, arg map[string]any) (tree *parse.Tree, err error) {\n\treturn createDocByMd(boxID, p, title, md, sorts, arg, true)\n}\n\nfunc createDocByMd(boxID, p, title, md string, sorts []string, arg map[string]any, syncWrite bool) (tree *parse.Tree, err error) {\n\tcreateDocLock.Lock()\n\tdefer createDocLock.Unlock()\n\n\tbox, err := getOpenedBox(boxID)\n\tif nil != err {\n\t\treturn\n\t}\n\tsortTargetID, _ := arg[\"sortTargetID\"].(string)\n\tsortPosition, _ := arg[\"sortPosition\"].(string)\n\tif \"\" != sortTargetID {\n\t\tif \"before\" != sortPosition && \"after\" != sortPosition {\n\t\t\treturn nil, fmt.Errorf(\"invalid sort position [%s]\", sortPosition)\n\t\t}\n\t\ttarget := treenode.GetBlockTree(sortTargetID)\n\t\tif !isSortableDocument(target) || target.BoxID != boxID || path.Dir(target.Path) != path.Dir(p) {\n\t\t\treturn nil, fmt.Errorf(\"sort target document [%s] is not a sibling of the new document\", sortTargetID)\n\t\t}\n\t} else if \"\" != sortPosition {\n\t\treturn nil, errors.New(\"sort target ID is required when sort position is specified\")\n\t}\n\n\tluteEngine := util.NewLute()\n\tluteEngine.SetHTMLTag2TextMark(true)\n\tdom := luteEngine.Md2BlockDOM(md, false)\n\ttree, err = createDoc0(box.ID, p, title, dom, false, syncWrite)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tFlushTxQueue()","sourceCodeStart":1283,"sourceCodeEnd":1319,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/file.go#L1283-L1319","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass sortPosition as exactly \"before\" or \"after\" when sortTargetID is provided","Omit sortPosition entirely when no positional placement is intended","Validate/sanitize the position value in the calling UI before invoking the API","Use lowercase ASCII keywords; do not localize the position strings"],"exampleFix":"// before\narg[\"sortPosition\"] = \"Before\"\n// after\narg[\"sortPosition\"] = \"before\"","handlingStrategy":"validation","validationCode":"function normalizeSortPosition(pos) {\n  if (pos == null || pos === \"\") return null; // no positional placement\n  const p = String(pos).toLowerCase();\n  if (p !== \"before\" && p !== \"after\") throw new Error(\"sortPosition must be 'before' or 'after'\");\n  return p;\n}","typeGuard":"function isValidSortPosition(v) {\n  return v === \"before\" || v === \"after\";\n}","tryCatchPattern":"try {\n  await createDocWithMd(args);\n} catch (e) {\n  if (/invalid sort position/.test(e.message)) {\n    delete args.sortPosition; delete args.sortTargetID; // retry with default placement\n    return createDocWithMd(args);\n  }\n  throw e;\n}","preventionTips":["Use only the literal strings \"before\"/\"after\"; never localized labels","Omit sortPosition when sortTargetID is absent","Add client-side validation before invoking the API"],"tags":["api","validation","document-creation","sort"],"backgroundTag":"invalid-enum-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}