{"record":{"id":"20c0376f72e5b7c3","repo":"ZhuLinsen/daily_stock_analysis","slug":"share-image-too-large","errorCode":"share_image_too_large","errorMessage":"报告内容超过分享图片上限 {max_chars} 字符","messagePattern":"报告内容超过分享图片上限 (.+?) 字符","errorType":"http","errorClass":"HTTPException","httpStatus":413,"severity":"warning","filePath":"api/v1/endpoints/history.py","lineNumber":835,"sourceCode":"    response_class=HTMLResponse,\n    responses={\n        200: {\"description\": \"供桌面端内置 Chromium 渲染的分享图 HTML\"},\n        404: {\"description\": \"报告不存在\", \"model\": ErrorResponse},\n        413: {\"description\": \"报告内容超过分享图长度上限\", \"model\": ErrorResponse},\n        500: {\"description\": \"报告生成失败\", \"model\": ErrorResponse},\n    },\n    summary=\"获取历史报告分享图 HTML\",\n    description=\"根据历史报告与持久化结构化数据生成只供桌面端本地截图的确定性 HTML\",\n)\ndef get_history_share_image_html(\n    record_id: str,\n    db_manager: DatabaseManager = Depends(get_database_manager),\n) -> HTMLResponse:\n    result, markdown_content = _history_share_image_input(record_id, db_manager)\n    config = get_config()\n    max_chars = getattr(config, \"markdown_to_image_max_chars\", 15000)\n    if len(markdown_content) > max_chars:\n        raise HTTPException(\n            status_code=413,\n            detail={\n                \"error\": \"share_image_too_large\",\n                \"message\": f\"报告内容超过分享图片上限 {max_chars} 字符\",\n            },\n        )\n\n    try:\n        html = build_share_image_html(\n            markdown_content,\n            structured_payload=_history_share_image_payload(result),\n            branding=_history_share_image_branding(config),\n        )\n    except Exception as exc:\n        logger.error(\"Share image HTML generation failed for %s: %s\", record_id, exc)\n        raise HTTPException(\n            status_code=500,\n            detail={","sourceCodeStart":817,"sourceCodeEnd":853,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/api/v1/endpoints/history.py#L817-L853","documentation":"413 share_image_too_large from GET /history/{record_id}/share-image-html. After loading the record and its markdown, the handler compares len(markdown_content) against config.markdown_to_image_max_chars (default 15000, overridable via configuration). Reports longer than the limit cannot be rendered into the deterministic share-image HTML, so the request is rejected before generation. This is a size guard, not a server fault.","triggerScenarios":"Requesting a share image for a long-form 'detailed' report whose markdown exceeds the configured cap; lowering markdown_to_image_max_chars in config while retaining large old reports; reports with very long news/strategy sections.","commonSituations":"Detailed analysis reports naturally exceeding 15k characters; environments that tuned the cap down for performance; desktop share flow hitting the limit only for specific stocks with dense content","solutions":["Raise markdown_to_image_max_chars in the service configuration (and restart/redeploy) if larger images are acceptable.","Generate the report as a shorter 'standard' report type and share that instead.","Fetch /history/{record_id}/markdown first, check its length, and only request the share image when under the limit.","If the limit is intentional, surface a friendly 'report too large to share as image' message in the client."],"exampleFix":"# before\nmax_chars = 15000  # default; detailed reports exceed it\n\n# after (config)\nmarkdown_to_image_max_chars = 30000","handlingStrategy":"validation","validationCode":"const md = await fetchMarkdown(id); // GET /history/{id}/markdown\nconst maxChars = 15000; // must mirror config markdown_to_image_max_chars\nif (md.content.length > maxChars) {\n  showTooLargeNotice(md.content.length, maxChars); // skip the share-image call\n}","typeGuard":null,"tryCatchPattern":"try { await getShareHtml(id); }\ncatch (e) { if (isHttp413(e)) showShareTooLarge(e.maxChars); }","preventionTips":["Check markdown length before requesting the share image.","Keep the client-side cap in sync with the server's markdown_to_image_max_chars config.","Prefer sharing shorter/standard reports as images; link the full report instead."],"tags":["http-413","share-image","config","size-limit"],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}