Coverage for src/qdrant_loader/core/file_conversion/exceptions.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.10.0, created at 2025-07-25 11:39 +0000

1"""Custom exceptions for file conversion operations.""" 

2 

3 

4 

5class FileConversionError(Exception): 

6 """Base exception for file conversion errors.""" 

7 

8 def __init__( 

9 self, 

10 message: str, 

11 file_path: str | None = None, 

12 file_type: str | None = None, 

13 ): 

14 """Initialize the exception. 

15 

16 Args: 

17 message: Error message 

18 file_path: Path to the file that failed conversion 

19 file_type: Type of file that failed conversion 

20 """ 

21 super().__init__(message) 

22 self.file_path = file_path 

23 self.file_type = file_type 

24 

25 

26class UnsupportedFileTypeError(FileConversionError): 

27 """Exception raised when file type is not supported for conversion.""" 

28 

29 def __init__(self, file_type: str, file_path: str | None = None): 

30 """Initialize the exception. 

31 

32 Args: 

33 file_type: The unsupported file type 

34 file_path: Path to the unsupported file 

35 """ 

36 message = f"File type '{file_type}' is not supported for conversion" 

37 super().__init__(message, file_path, file_type) 

38 

39 

40class FileSizeExceededError(FileConversionError): 

41 """Exception raised when file size exceeds the maximum allowed size.""" 

42 

43 def __init__(self, file_size: int, max_size: int, file_path: str | None = None): 

44 """Initialize the exception. 

45 

46 Args: 

47 file_size: Actual file size in bytes 

48 max_size: Maximum allowed file size in bytes 

49 file_path: Path to the oversized file 

50 """ 

51 message = ( 

52 f"File size {file_size} bytes exceeds maximum allowed size {max_size} bytes" 

53 ) 

54 super().__init__(message, file_path) 

55 self.file_size = file_size 

56 self.max_size = max_size 

57 

58 

59class ConversionTimeoutError(FileConversionError): 

60 """Exception raised when file conversion times out.""" 

61 

62 def __init__(self, timeout: int, file_path: str | None = None): 

63 """Initialize the exception. 

64 

65 Args: 

66 timeout: Timeout duration in seconds 

67 file_path: Path to the file that timed out 

68 """ 

69 message = f"File conversion timed out after {timeout} seconds" 

70 super().__init__(message, file_path) 

71 self.timeout = timeout 

72 

73 

74class MarkItDownError(FileConversionError): 

75 """Exception raised when MarkItDown library fails.""" 

76 

77 def __init__( 

78 self, 

79 original_error: Exception, 

80 file_path: str | None = None, 

81 file_type: str | None = None, 

82 ): 

83 """Initialize the exception. 

84 

85 Args: 

86 original_error: The original exception from MarkItDown 

87 file_path: Path to the file that failed conversion 

88 file_type: Type of file that failed conversion 

89 """ 

90 message = f"MarkItDown conversion failed: {str(original_error)}" 

91 super().__init__(message, file_path, file_type) 

92 self.original_error = original_error 

93 

94 

95class FileAccessError(FileConversionError): 

96 """Exception raised when file cannot be accessed or read.""" 

97 

98 def __init__(self, file_path: str, original_error: Exception | None = None): 

99 """Initialize the exception. 

100 

101 Args: 

102 file_path: Path to the inaccessible file 

103 original_error: The original exception that caused the access error 

104 """ 

105 message = f"Cannot access file: {file_path}" 

106 if original_error: 

107 message += f" - {str(original_error)}" 

108 super().__init__(message, file_path) 

109 self.original_error = original_error