Reuse error messages for missing dependencies.

This commit is contained in:
Adam Fourney 2025-02-28 20:28:35 -08:00
parent 98698a64ce
commit e5dc512948
3 changed files with 19 additions and 14 deletions

View file

@ -1,5 +1,12 @@
from typing import Optional, List, Any from typing import Optional, List, Any
MISSING_DEPENDENCY_MESSAGE = """{converter} recognized the input as a potential {extension} file, but the dependencies needed to read {extension} files have not been installed. To resolve this error, include the optional dependency [{feature}] or [all] when installing MarkItDown. For example:
* pip install markitdown[{feature}]
* pip install markitdown[all]
* pip install markitdown[{feature}, ...]
* etc."""
class MarkItDownException(Exception): class MarkItDownException(Exception):
""" """

View file

@ -8,7 +8,7 @@ from ._base import (
from ._base import DocumentConverter from ._base import DocumentConverter
from ._html_converter import HtmlConverter from ._html_converter import HtmlConverter
from .._exceptions import MissingDependencyException from .._exceptions import MissingDependencyException, MISSING_DEPENDENCY_MESSAGE
# Try loading optional (but in this case, required) dependencies # Try loading optional (but in this case, required) dependencies
# Save reporting of any exceptions for later # Save reporting of any exceptions for later
@ -39,12 +39,11 @@ class DocxConverter(HtmlConverter):
# Load the dependencies # Load the dependencies
if _dependency_exc_info is not None: if _dependency_exc_info is not None:
raise MissingDependencyException( raise MissingDependencyException(
f"""{type(self).__name__} recognized the input as a potential .docx file, but the dependencies needed to read .docx files have not been installed. To resolve this error, include the optional dependency [docx] or [all] when installing MarkItDown. For example: MISSING_DEPENDENCY_MESSAGE.format(
converter=type(self).__name__,
* pip install markitdown[docx] extension=".docx",
* pip install markitdown[all] feature="docx",
* pip install markitdown[pptx, docx, ...] )
* etc."""
) from _dependency_exc_info[1].with_traceback( ) from _dependency_exc_info[1].with_traceback(
_dependency_exc_info[2] _dependency_exc_info[2]
) # Restore the original traceback ) # Restore the original traceback

View file

@ -7,7 +7,7 @@ from typing import Union
from ._base import DocumentConverterResult, DocumentConverter from ._base import DocumentConverterResult, DocumentConverter
from ._html_converter import HtmlConverter from ._html_converter import HtmlConverter
from .._exceptions import MissingDependencyException from .._exceptions import MissingDependencyException, MISSING_DEPENDENCY_MESSAGE
# Try loading optional (but in this case, required) dependencies # Try loading optional (but in this case, required) dependencies
# Save reporting of any exceptions for later # Save reporting of any exceptions for later
@ -67,12 +67,11 @@ class PptxConverter(HtmlConverter):
# Load the dependencies # Load the dependencies
if _dependency_exc_info is not None: if _dependency_exc_info is not None:
raise MissingDependencyException( raise MissingDependencyException(
f"""{type(self).__name__} recognized the input as a potential .pptx file, but the dependencies needed to read .pptx files have not been installed. To resolve this error, include the optional dependency [pptx] or [all] when installing MarkItDown. For example: MISSING_DEPENDENCY_MESSAGE.format(
converter=type(self).__name__,
* pip install markitdown[pptx] extension=".pptx",
* pip install markitdown[all] feature="pptx",
* pip install markitdown[pptx, docx, ...] )
* etc."""
) from _dependency_exc_info[1].with_traceback( ) from _dependency_exc_info[1].with_traceback(
_dependency_exc_info[2] _dependency_exc_info[2]
) # Restore the original traceback ) # Restore the original traceback