{"record":{"id":"2cb56b8503c5cefd","repo":"binary-husky/gpt_academic","slug":"pdf-str-e","errorCode":null,"errorMessage":"转换PDF失败: {str(e)}","messagePattern":"转换PDF失败: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"crazy_functions/doc_fns/conversation_doc/word2pdf.py","lineNumber":86,"sourceCode":"                    raise RuntimeError(\"PDF生成失败或文件为空\")\n\n                print(f\"PDF转换成功，文件大小: {pdf_path.stat().st_size} 字节\")\n            else:\n                # Windows和MacOS使用docx2pdf\n                print(f\"使用docx2pdf转换 {word_path} 到 {pdf_path}\")\n                convert(word_path, pdf_path)\n\n                # 验证PDF是否成功生成\n                if not pdf_path.exists() or pdf_path.stat().st_size == 0:\n                    raise RuntimeError(\"PDF生成失败或文件为空\")\n\n                print(f\"PDF转换成功，文件大小: {pdf_path.stat().st_size} 字节\")\n\n            return str(pdf_path)\n\n        except Exception as e:\n            print(f\"PDF转换异常: {str(e)}\")\n            raise Exception(f\"转换PDF失败: {str(e)}\")\n\n    @staticmethod\n    def batch_convert(word_dir: Union[str, Path], pdf_dir: Union[str, Path] = None) -> list:\n        \"\"\"\n        批量转换目录下的所有Word文档\n\n        参数:\n            word_dir: 包含Word文档的目录路径\n            pdf_dir: 可选，PDF文件的输出目录。如果未指定，将使用与Word文档相同的目录\n\n        返回:\n            生成的PDF文件路径列表\n        \"\"\"\n        word_dir = Path(word_dir)\n        if pdf_dir:\n            pdf_dir = Path(pdf_dir)\n            pdf_dir.mkdir(parents=True, exist_ok=True)\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/doc_fns/conversation_doc/word2pdf.py#L68-L104","documentation":"The outer catch-all of WordToPdfConverter.convert_to_pdf: any exception escaping the platform branches (missing LibreOffice at [22], conversion failure at [23], empty PDF at [24]/[25], or anything else) is printed and re-raised as a generic Exception('转换PDF失败: <original>'). The original type is lost (bare `raise Exception`), so callers cannot distinguish root causes without string matching; the original message is preserved only as text.","triggerScenarios":"Any caller of convert_to_pdf() hitting the underlying errors [22]-[25], or unexpected ones like an invalid word_path (not a Path, suffix fails), pdf_path.parent creation permission errors, or os.rename cross-device failure.","commonSituations":"Developers see only the wrapped message in logs and must trace the preceding 'PDF转换异常:' print to identify the real cause; try/except ValueError style guards around convert_to_pdf never fire because everything is re-raised as bare Exception.","solutions":["Read the message suffix — it is the original error text (e.g. '请先安装LibreOffice...' means error 22); the printed 'PDF转换异常:' line above the raise carries the same info","Fix the underlying cause per errors 22-25","In your own wrapper, dispatch on message content or stop calling this API and call platform-specific tools directly","If patching the library: use `raise` (bare) or `raise RuntimeError(...) from e` to preserve the cause chain"],"exampleFix":"// before\nexcept Exception as e:\n    print(f\"PDF转换异常: {str(e)}\")\n    raise Exception(f\"转换PDF失败: {str(e)}\")\n\n// after\nexcept Exception as e:\n    print(f\"PDF转换异常: {str(e)}\")\n    raise RuntimeError(f\"转换PDF失败: {str(e)}\") from e","handlingStrategy":"try-catch","validationCode":"from pathlib import Path\nimport platform, shutil\n\ndef precheck_word2pdf(word_path) -> list[str]:\n    problems = []\n    p = Path(word_path)\n    if not p.exists(): problems.append('missing file')\n    if platform.system() == 'Linux' and shutil.which('libreoffice') is None:\n        problems.append('libreoffice missing')\n    return problems","typeGuard":null,"tryCatchPattern":"try:\n    pdf = WordToPdfConverter.convert_to_pdf(word_path)\nexcept Exception as e:          # everything is re-raised as bare Exception\n    msg = str(e)\n    if '请先安装LibreOffice' in msg: ...\n    elif 'LibreOffice转换失败' in msg: ...\n    elif 'PDF生成失败' in msg: ...\n    else: raise","preventionTips":["This wrapper flattens all underlying errors to Exception — dispatch on message substrings, or patch to re-raise with `from e`","Run the preflight checks (file exists, converter present) yourself before calling","Log the printed 'PDF转换异常:' line; it mirrors the original error"],"tags":["exception-wrapping","pdf-conversion","error-handling","python"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}