{"record":{"id":"b589bd89da2342f0","repo":"siyuan-note/siyuan","slug":"read-body-failed","errorCode":null,"errorMessage":"read body failed: ","messagePattern":"read body failed: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/httprequest.go","lineNumber":356,"sourceCode":"\t\treturn 0, \"\", \"\", errors.New(\"nil response\")\n\t}\n\tdefer resp.Body.Close()\n\n\tstatusCode = resp.StatusCode\n\tcontentType = resp.Header.Get(\"Content-Type\")\n\n\tmaxReadBytes := int64(maxHTTPRequestBytes)\n\tif !isTextContentType(contentType) {\n\t\tmaxReadBytes = maxHTTPRequestFileBytes\n\t}\n\t// ContentLength 为 -1（chunked）时跳过大小预检，交由 LimitReader 兜底截断。\n\tif resp.ContentLength > maxReadBytes {\n\t\treturn statusCode, contentType, \"\", errors.New(\"response too large\")\n\t}\n\n\trespBody, rerr := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))\n\tif rerr != nil {\n\t\treturn statusCode, contentType, \"\", errors.New(\"read body failed: \" + rerr.Error())\n\t}\n\n\t// 二进制响应落盘，返回文件路径，供智能体按需进一步处理。\n\tif !isTextContentType(contentType) {\n\t\timportDir := filepath.Join(TempDir, \"import\")\n\t\tif merr := os.MkdirAll(importDir, 0755); merr != nil {\n\t\t\treturn statusCode, contentType, \"\", 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, respBody, 0644); werr != nil {\n\t\t\treturn statusCode, contentType, \"\", errors.New(\"write file failed: \" + werr.Error())\n\t\t}\n\t\treturn statusCode, contentType, fmt.Sprintf(\"Saved to: %s (%d bytes)\", filePath, len(respBody)), nil\n\t}\n\n\treturn statusCode, contentType, truncateRunes(string(respBody), maxHTTPRequestChars), nil\n}","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/httprequest.go#L338-L374","documentation":"After passing the size precheck, HTTPRequest reads the response body with io.ReadAll(io.LimitReader(...)). This error wraps any failure during that read — typically a mid-stream network drop, a timeout while streaming, or decompression/transfer-encoding errors — with the underlying message appended after \"read body failed: \".","triggerScenarios":"Calling HTTPRequest when the connection is reset or times out while the body is streaming; the server closes the connection prematurely; a proxy/CDN truncates the transfer; gzip/deflate body corruption; context cancellation mid-read.","commonSituations":"Flaky mobile/VPN networks; servers with aggressive idle timeouts on slow links; overloaded endpoints aborting large-ish responses; intermediaries (nginx, Cloudflare) cutting the stream; laptop sleep during a fetch.","solutions":["Read the appended underlying error to distinguish timeout vs connection reset vs decompression failure","Retry the request — transient network interruptions are the most common cause","Increase relevant timeouts if the response is legitimately large and slow to stream","Check intermediary proxies/CDNs for truncation and verify server stability","Ensure the caller's context is not being cancelled prematurely"],"exampleFix":"// before\nctx := context.Background() // no deadline, but network drops mid-body\n// after — retry on transient read failure\nfor i := 0; i < 3; i++ {\n    _, _, out, err := util.HTTPRequest(ctx, url)\n    if err == nil || !strings.HasPrefix(err.Error(), \"read body failed:\") { break }\n    time.Sleep(time.Second)\n}","handlingStrategy":"retry","validationCode":"if !endpointHealthy(target) { return fmt.Errorf(\"endpoint unstable, skipping\") }","typeGuard":null,"tryCatchPattern":"if err != nil && strings.HasPrefix(err.Error(), \"read body failed:\") {\n    time.Sleep(backoff)\n    // retry up to N times; surface a friendly message on exhaustion\n}","preventionTips":["Retry transient stream errors with exponential backoff","Use stable networks or increase timeouts for slow links","Avoid fetching through unreliable proxies"],"tags":["network","http","io","go"],"backgroundTag":"http-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"}