{"record":{"id":"e402d6ee9d2cbc79","repo":"chenhg5/cc-connect","slug":"range-request-status-d-body-q","errorCode":null,"errorMessage":"range request status=%d body=%q","messagePattern":"range request status=(.+?) body=%q","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/feishu/resource_download.go","lineNumber":375,"sourceCode":"\n\t\t// Non-206 response: capture a snippet for diagnostics, then either\n\t\t// retry (transient 5xx) or fail fast.\n\t\tbody, _ := io.ReadAll(io.LimitReader(resp.Body, 4*1024))\n\t\t_ = resp.Body.Close()\n\n\t\tif resp.StatusCode >= 500 && resp.StatusCode < 600 && attempt < maxTransientRetries {\n\t\t\tlastErr = fmt.Errorf(\"range status %d body=%q\", resp.StatusCode, strings.TrimSpace(string(body)))\n\t\t\tcontinue\n\t\t}\n\n\t\tif resp.StatusCode >= 200 && resp.StatusCode < 300 && end-start+1 == int64(len(body)) {\n\t\t\t// Server ignored Range and returned a 200 with exactly the bytes\n\t\t\t// we wanted — fine for the last chunk of a file, slightly off for\n\t\t\t// non-tail chunks. Caller decides; we return what we got.\n\t\t\treturn body, nil\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"range request status=%d body=%q\", resp.StatusCode, strings.TrimSpace(string(body)))\n\t}\n\n\tif lastErr == nil {\n\t\tlastErr = errors.New(\"range chunk retries exhausted\")\n\t}\n\treturn nil, fmt.Errorf(\"range chunk retries exhausted: %w\", lastErr)\n}\n\n// defaultResourceChunkSize returns the chunk size used when a Platform is\n// constructed without going through newPlatform (tests, manual fixtures).\n// Matches the production default of 8 MiB.\nfunc defaultResourceChunkSize() int64 { return 8 * 1024 * 1024 }\n\n// fetchResourceTokenOrDefault returns the bearer token used for resource\n// downloads. Tests can inject a stub via Platform.fetchResourceToken;\n// production callers fall through to fetchFreshTenantAccessToken which uses\n// the lark SDK to mint a fresh tenant token on demand.\nfunc (p *Platform) fetchResourceTokenOrDefault(ctx context.Context) (string, error) {","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/feishu/resource_download.go#L357-L393","documentation":"Final failure path of resourceRangeChunk: a ranged request returned a status that is neither a usable 206 nor a full-body 200 with the exact requested length. The error carries the status code and a 4 KiB body snippet so the caller can see why the server rejected the range (auth, permission, not found, bad request, etc.).","triggerScenarios":"resourceFetchRemainingChunks -> resourceRangeChunk: response status is e.g. 401/403/404/416 or any non-2xx, or a 2xx whose body length does not match end-start+1; no retry applies so the error is returned immediately.","commonSituations":"Expired/insufficient drive read permission (403); file deleted or token revoked (404); invalid Range triggering 416; tenant policy blocking download; auth token expiring mid multi-chunk download.","solutions":["Read the status code and body snippet: 401/403 → refresh or re-grant drive read credentials; 404 → re-fetch file metadata (file deleted/moved); 416 → recompute the range against a fresh file size","Re-run the full download to obtain fresh metadata and tokens before resuming chunk fetches","Verify the app/scopes have drive:drive.readonly (or equivalent) permission for the file","If it happens mid-download repeatedly, shorten total download time or re-authenticate between chunks"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before downloading, verify the app can still access the file\nmeta, err := fetchFileMeta(fileToken)\nif err != nil { return fmt.Errorf(\"file %s not accessible: %w\", fileToken, err) }","typeGuard":null,"tryCatchPattern":"var httpErr *HTTPStatusError\nif errors.As(err, &httpErr) {\n    switch httpErr.StatusCode {\n    case 401: refreshToken(); retry()\n    case 403: requestDriveReadScope()\n    case 404: reFetchFileMeta()\n    case 416: recomputeRanges()\n    }\n}","preventionTips":["Grant the app drive read scope for every file it downloads","Refresh auth tokens before long multi-chunk downloads","Re-check file existence before resuming a saved partial download","Log status + body snippet to distinguish permission vs deletion issues"],"tags":["http","feishu","range-request","authorization"],"backgroundTag":"http-non-2xx-response","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}