{"record":{"id":"f80535c823dae2aa","repo":"siyuan-note/siyuan","slug":"parses-request-s-failed-s","errorCode":null,"errorMessage":"Parses request [%s] failed: %s","messagePattern":"Parses request \\[(.+?)\\] failed: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/apicontract/block_remaining.go","lineNumber":61,"sourceCode":"// 特殊解码仅保留这些入口的条件参数和结构体绑定语义，响应仍受各自的契约约束。\nfunc init() {\n\tCheckBlockRef.decodeRequest = decodeCheckBlockRef\n\tGetHeadingLevelTransaction.decodeRequest = decodeHeadingLevel\n\tGetDocHeadingLevelTransaction.decodeRequest = func(reader io.Reader) (request DocHeadingLevelRequest, err error) {\n\t\tif json.NewDecoder(reader).Decode(&request) != nil {\n\t\t\terr = errors.New(\"invalid heading conversion parameters\")\n\t\t}\n\t\treturn\n\t}\n}\n\nfunc blockRequestFields(reader io.Reader, path string) (fields map[string]json.RawMessage, err error) {\n\terr = json.NewDecoder(reader).Decode(&fields)\n\tif err != nil {\n\t\tif errors.Is(err, io.EOF) {\n\t\t\terr = errors.New(\"the request body is empty or truncated (EOF)\")\n\t\t}\n\t\terr = fmt.Errorf(\"Parses request [%s] failed: %s\", path, err)\n\t}\n\treturn\n}\n\nfunc decodeCheckBlockRef(reader io.Reader) (request CheckBlockRefRequest, err error) {\n\tfields, err := blockRequestFields(reader, \"/api/block/checkBlockRef\")\n\tif err != nil {\n\t\treturn request, err\n\t}\n\tif raw, present := fields[\"scope\"]; present {\n\t\tif bytes.Equal(raw, []byte(\"null\")) || json.Unmarshal(raw, &request.Scope) != nil {\n\t\t\treturn request, errors.New(\"Field [scope] should be of type [String]\")\n\t\t}\n\t}\n\tif strings.TrimSpace(request.Scope) == \"\" {\n\t\trequest.Scope = \"blocks\"\n\t}\n\treadArray := func(key string, required, nonempty bool) ([]string, error) {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/apicontract/block_remaining.go#L43-L79","documentation":"Generic request parse failure for all endpoints that decode their body into a field map via blockRequestFields. The message embeds the API path (%s) and the underlying json error, e.g. 'Parses request [/api/block/checkBlockRef] failed: invalid character ...'. It means the body was readable but is not valid JSON or not a JSON object.","triggerScenarios":"POSTing malformed JSON (trailing commas, single quotes, unquoted keys, concatenated objects) to any blockRequestFields-backed endpoint such as /api/block/checkBlockRef, heading-level transactions, bazaar, or AV requests.","commonSituations":"Hand-built payload strings with syntax errors, templates/interpolation injecting raw values, text/plain or form-encoded bodies sent to a JSON endpoint, or a proxy rewriting the body.","solutions":["Validate the body with a JSON parser before sending and fix syntax errors.","Use Content-Type: application/json and a real serializer instead of string concatenation.","Read the embedded json error after 'failed:' — it pinpoints the exact character/offset.","Compare against a known-good request example from the API docs."],"exampleFix":"// before\n{\"id\": \"x\",} // trailing comma\n// after\n{\"id\": \"x\"}","handlingStrategy":"validation","validationCode":"try { JSON.parse(bodyString); } catch (e) { throw new Error(\"request body is not valid JSON: \" + e.message); }","typeGuard":null,"tryCatchPattern":"try {\n  await post(path, body);\n} catch (e) {\n  if (String(e.message).startsWith(\"Parses request\")) {\n    // embedded json error after 'failed:' names the bad character/offset\n    console.error(\"Malformed JSON for\", path, bodyString);\n  } else { throw e; }\n}","preventionTips":["Use JSON.stringify exclusively; never concatenate payload strings","Interpolate values safely (watch quotes and unescaped chars)","Send Content-Type: application/json","Lint request fixtures used in tests"],"tags":["json","parsing","http","api"],"backgroundTag":"json-parse-error","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"}