File size: 6,005 Bytes
7cfdc4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from __future__ import annotations
from typing import Any, Dict, Optional


class FastParseError(Exception):
    def __init__(
        self,
        message: str,
        error_code: str = "FAST_PARSE_ERROR",
        details: Optional[Dict[str, Any]] = None,
        recoverable: bool = True,
    ):
        super().__init__(message)
        self.message = message
        self.error_code = error_code
        self.details = details or {}
        self.recoverable = recoverable
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            "error": self.error_code,
            "message": self.message,
            "details": self.details,
            "recoverable": self.recoverable,
        }


class FileSizeExceededError(FastParseError):
    def __init__(self, file_size: int, max_size: int, filename: str = ""):
        super().__init__(
            message=f"File size ({file_size:,} bytes) exceeds maximum allowed ({max_size:,} bytes)",
            error_code="FILE_SIZE_EXCEEDED",
            details={
                "file_size": file_size,
                "max_size": max_size,
                "filename": filename,
            },
            recoverable=False,
        )


class UnsupportedFormatError(FastParseError):
    def __init__(self, extension: str, mime_type: Optional[str] = None):
        super().__init__(
            message=f"Unsupported file format: {extension}",
            error_code="UNSUPPORTED_FORMAT",
            details={
                "extension": extension,
                "mime_type": mime_type,
            },
            recoverable=False,
        )


class CorruptedFileError(FastParseError):
    def __init__(self, filename: str, file_type: str, original_error: Optional[str] = None):
        super().__init__(
            message=f"File appears to be corrupted or invalid: {filename}",
            error_code="CORRUPTED_FILE",
            details={
                "filename": filename,
                "file_type": file_type,
                "original_error": original_error,
            },
            recoverable=False,
        )


class EncodingError(FastParseError):
    def __init__(self, filename: str, detected_encoding: Optional[str] = None):
        super().__init__(
            message=f"Failed to decode file content: {filename}",
            error_code="ENCODING_ERROR",
            details={
                "filename": filename,
                "detected_encoding": detected_encoding,
            },
            recoverable=True,
        )


class DependencyMissingError(FastParseError):
    def __init__(self, dependency: str, file_type: str):
        super().__init__(
            message=f"Required dependency '{dependency}' not installed for {file_type} parsing",
            error_code="DEPENDENCY_MISSING",
            details={
                "dependency": dependency,
                "file_type": file_type,
            },
            recoverable=False,
        )


class SecurityWarningError(FastParseError):
    def __init__(self, filename: str, patterns_found: list):
        super().__init__(
            message=f"Potentially dangerous content detected in: {filename}",
            error_code="SECURITY_WARNING",
            details={
                "filename": filename,
                "patterns_found": patterns_found,
            },
            recoverable=True,
        )


class TimeoutError(FastParseError):
    def __init__(self, operation: str, timeout_seconds: float):
        super().__init__(
            message=f"Operation '{operation}' timed out after {timeout_seconds}s",
            error_code="TIMEOUT",
            details={
                "operation": operation,
                "timeout_seconds": timeout_seconds,
            },
            recoverable=True,
        )


class ImageAnalysisError(FastParseError):
    def __init__(self, filename: str, reason: str):
        super().__init__(
            message=f"Image analysis failed for {filename}: {reason}",
            error_code="IMAGE_ANALYSIS_ERROR",
            details={
                "filename": filename,
                "reason": reason,
            },
            recoverable=True,
        )


class FileNotFoundError(FastParseError):
    def __init__(self, file_path: str):
        super().__init__(
            message=f"File not found: {file_path}",
            error_code="FILE_NOT_FOUND",
            details={
                "file_path": file_path,
            },
            recoverable=False,
        )


class TruncationWarning(FastParseError):
    def __init__(self, filename: str, original_size: int, truncated_size: int, unit: str = "chars"):
        super().__init__(
            message=f"Content truncated from {original_size:,} to {truncated_size:,} {unit}",
            error_code="CONTENT_TRUNCATED",
            details={
                "filename": filename,
                "original_size": original_size,
                "truncated_size": truncated_size,
                "unit": unit,
            },
            recoverable=True,
        )


def classify_exception(e: Exception, filename: str = "") -> FastParseError:
    if isinstance(e, FastParseError):
        return e
    
    error_msg = str(e).lower()
    
    if "corrupted" in error_msg or "invalid" in error_msg or "malformed" in error_msg:
        return CorruptedFileError(filename, "unknown", str(e))
    
    if "decode" in error_msg or "encoding" in error_msg or "unicode" in error_msg:
        return EncodingError(filename)
    
    if "not found" in error_msg or "does not exist" in error_msg:
        return FileNotFoundError(filename)
    
    if "timeout" in error_msg or "timed out" in error_msg:
        return TimeoutError("parse", 0)
    
    if "import" in error_msg or "module" in error_msg:
        return DependencyMissingError("unknown", "unknown")
    
    return FastParseError(
        message=str(e),
        error_code="UNKNOWN_ERROR",
        details={"original_type": type(e).__name__, "filename": filename},
    )