666ghj/MiroFish · error · ValueError
无法处理的文件格式: {suffix}
Error message
无法处理的文件格式: {suffix} What it means
A defensive, effectively unreachable ValueError at the end of the dispatch chain: the suffix was already validated against SUPPORTED_EXTENSIONS and every supported suffix (.pdf, .md/.markdown, .txt) is handled by an explicit branch, so control cannot reach this raise unless SUPPORTED_EXTENSIONS and the if/elif chain drift out of sync.
Source
Thrown at backend/app/utils/file_parser.py:108
"""
path = Path(file_path)
if not path.exists():
raise FileNotFoundError(f"文件不存在: {file_path}")
suffix = path.suffix.lower()
if suffix not in cls.SUPPORTED_EXTENSIONS:
raise ValueError(f"不支持的文件格式: {suffix}")
if suffix == '.pdf':
return cls._extract_from_pdf(file_path)
elif suffix in {'.md', '.markdown'}:
return cls._extract_from_md(file_path)
elif suffix == '.txt':
return cls._extract_from_txt(file_path)
raise ValueError(f"无法处理的文件格式: {suffix}")
@staticmethod
def _extract_from_pdf(file_path: str) -> str:
"""从PDF提取文本"""
try:
import fitz # PyMuPDF
except ImportError:
raise ImportError("需要安装PyMuPDF: pip install PyMuPDF")
text_parts = []
with fitz.open(file_path) as doc:
for page in doc:
text = page.get_text()
if text.strip():
text_parts.append(text)
return "\n\n".join(text_parts)
View on GitHub (pinned to b5b53acc57)
Solutions
- Find which extension passed SUPPORTED_EXTENSIONS but has no branch, and either remove it from the set or add the extractor
- Refactor to a dispatch dict {suffix: extractor} so the allow-list and dispatch cannot diverge
- Add a unit test that asserts every entry in SUPPORTED_EXTENSIONS has a registered extractor
Example fix
# before
SUPPORTED_EXTENSIONS = {'.pdf', '.md', '.markdown', '.txt', '.rtf'}
# no .rtf branch -> falls through to unreachable raise
# after
_EXTRACTORS = {'.pdf': cls._extract_from_pdf, '.md': cls._extract_from_md,
'.markdown': cls._extract_from_md, '.txt': cls._extract_from_txt}
SUPPORTED_EXTENSIONS = set(_EXTRACTORS)
extractor = _EXTRACTORS.get(suffix)
if extractor is None:
raise ValueError(f"不支持的文件格式: {suffix}")
return extractor(file_path) Defensive patterns
Strategy: validation
Validate before calling
assert FileParser.SUPPORTED_EXTENSIONS <= {'/.pdf', '.pdf', '.md', '.markdown', '.txt'}, "extension set drifted from dispatch" Prevention
- Refactor to a suffix-to-extractor dict so the allow-list cannot drift from dispatch
- Add a unit test iterating SUPPORTED_EXTENSIONS through extract_text with dummy files
- Code-review rule: adding an extension requires adding its extractor in the same commit
When it happens
Trigger: Someone adds an extension to SUPPORTED_EXTENSIONS (e.g. '.rtf') without adding a matching _extract_from_rtf branch — the new suffix passes the gate check, matches no elif, and falls through to this raise.
Common situations: Maintenance drift: the allow-list and the dispatcher are two separate structures that must be updated together; this error is the canary that they diverged.
Related errors
AI-assisted analysis of 666ghj/MiroFish@b5b53acc57 (2026-08-14).
Data as JSON: /api/errors/a22a7c1d3d3053b0.
Report an issue: GitHub.