{"record":{"id":"a91247c94c0f4b25","repo":"siyuan-note/siyuan","slug":"read-body-failed-s-a91247","errorCode":null,"errorMessage":"read body failed: %s","messagePattern":"read body failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/webfetch.go","lineNumber":74,"sourceCode":"\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode >= 400 {\n\t\treturn \"\", fmt.Errorf(\"HTTP %d\", resp.StatusCode)\n\t}\n\n\tcontentType := resp.Header.Get(\"Content-Type\")\n\tmaxReadBytes := int64(maxWebFetchBytes)\n\tif !strings.HasPrefix(contentType, \"text/html\") && !strings.HasPrefix(contentType, \"text/plain\") {\n\t\tmaxReadBytes = maxWebFetchFileBytes\n\t}\n\tif resp.ContentLength > maxReadBytes {\n\t\treturn \"\", errors.New(\"response too large\")\n\t}\n\n\tbody, err := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))\n\tif err != nil {\n\t\treturn \"\", errors.New(\"read body failed: \" + err.Error())\n\t}\n\n\tif !strings.HasPrefix(contentType, \"text/html\") && !strings.HasPrefix(contentType, \"text/plain\") {\n\t\timportDir := filepath.Join(TempDir, \"import\")\n\t\tif merr := os.MkdirAll(importDir, 0755); merr != nil {\n\t\t\treturn \"\", errors.New(\"create import dir failed: \" + merr.Error())\n\t\t}\n\t\tfilename := extractFilename(rawURL, contentType)\n\t\tfilePath := filepath.Join(importDir, filename)\n\t\tif werr := os.WriteFile(filePath, body, 0644); werr != nil {\n\t\t\treturn \"\", errors.New(\"write file failed: \" + werr.Error())\n\t\t}\n\t\treturn fmt.Sprintf(\"Saved to: %s (%d bytes)\", filePath, len(body)), nil\n\t}\n\n\thtmlStr := string(body)\n\n\tisHTML := strings.HasPrefix(contentType, \"text/html\")","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/webfetch.go#L56-L92","documentation":"WebFetch reads the response body via io.ReadAll(io.LimitReader(resp.Body, maxReadBytes)); any I/O error during that read (connection reset mid-body, TLS error, timeout while streaming, truncated chunked encoding) is wrapped as 'read body failed: <cause>'. Headers were received fine; the body transfer broke.","triggerScenarios":"Server closes the connection partway through the body; network interruption during a large transfer; read deadline hit on a slow body; proxy dropping a keep-alive connection mid-response.","commonSituations":"Flaky mobile/VPN connections; aggressive load balancers killing slow responses; fetching from servers that trickle data and trip timeouts; transient upstream failures behind CDNs.","solutions":["Retry the fetch — this is usually transient","Inspect the wrapped cause after 'read body failed: ' for reset vs timeout specifics","Prefer smaller, faster endpoints (the shorter the transfer, the lower the interruption risk)","If persistent, check for middleboxes/proxies between the kernel and the target and raise client timeouts"],"exampleFix":"// before\ncontent, err := WebFetch(url, \"markdown\")\n\n// after\ncontent, err := WebFetch(url, \"markdown\")\nif err != nil && strings.Contains(err.Error(), \"read body failed\") {\n    content, err = WebFetch(url, \"markdown\") // one retry for transient truncation\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"content, err := WebFetch(url, format)\nif err != nil && strings.Contains(err.Error(), \"read body failed\") {\n    time.Sleep(time.Second)\n    content, err = WebFetch(url, format) // single retry for transient truncation\n}","preventionTips":["Retry once or twice — mid-body resets are usually transient","Avoid fetching from known-flaky sources over unstable links","Prefer smaller endpoints to shorten the transfer window","Investigate proxies/middleboxes if resets cluster on one network path"],"tags":["network","io","http"],"backgroundTag":"network-request-failed","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"}