File size: 13,720 Bytes
2f203f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Centralized error management for the Data360 MCP server.

Provides a unified error hierarchy for consistent, LLM-actionable error messages.
Follows the pattern from https://github.com/avsolatorio/data-ai-chatbot/blob/dev/backend/app/core/errors.py

Error codes follow the format: "<type>:<context>" (e.g. "http_error:search", "timeout:metadata").
"""

import logging
from typing import Any

_logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Message registry - maps error codes to user/LLM-friendly messages
# ---------------------------------------------------------------------------
_ERROR_MESSAGES: dict[str, str] = {
    # HTTP / network errors
    "http_error:search": "The search request failed with an HTTP error. Please try again.",
    "http_error:dataset": "The dataset search request failed with an HTTP error. Please try again.",
    "http_error:metadata": "Failed to fetch metadata due to an HTTP error. Verify the indicator_id and database_id are correct.",
    "http_error:disaggregation": "Failed to fetch disaggregation options due to an HTTP error. Verify the indicator_id and database_id are correct.",
    "http_error:data": "Failed to fetch data due to an HTTP error. Verify the indicator_id, database_id, and filters are correct.",
    # Timeouts
    "timeout:search": "The search request timed out. Please try again.",
    "timeout:dataset": "The dataset search request timed out. Please try again.",
    "timeout:metadata": "The metadata request timed out. Please try again.",
    "timeout:disaggregation": "The disaggregation request timed out. Please try again.",
    "timeout:data": "The data request timed out. Please try again.",
    # Request errors (connection issues, DNS, etc.)
    "request_error:search": "A network error occurred during search. Check connectivity and try again.",
    "request_error:dataset": "A network error occurred searching datasets. Check connectivity and try again.",
    "request_error:metadata": "A network error occurred fetching metadata. Check connectivity and try again.",
    "request_error:disaggregation": "A network error occurred fetching disaggregation options. Check connectivity and try again.",
    "request_error:data": "A network error occurred fetching data. Check connectivity and try again.",
    # Parse errors
    "parse_error:search": "Failed to parse the search API response. The upstream API may be returning unexpected data.",
    "parse_error:dataset": "Failed to parse the dataset search response.",
    "parse_error:metadata": "Failed to parse the metadata API response.",
    "parse_error:disaggregation": "Failed to parse the disaggregation API response.",
    "parse_error:data": "Failed to parse the data API response.",
    # Validation errors
    "validation_error:search": "Invalid search parameters. Please check your query and filters.",
    "validation_error:dataset": "Invalid dataset search parameters.",
    "validation_error:metadata": "Invalid metadata request parameters. Check the indicator_id and database_id.",
    "validation_error:data": "Invalid data request parameters. Check the indicator_id, database_id, and filters.",
    "validation_error:api_response": "The API response failed validation. The data format may have changed.",
    # Not found
    "not_found:indicator": "No indicators found matching your query. Try broadening your search terms.",
    "not_found:metadata": "No metadata found for the specified indicator. Verify the indicator_id is correct.",
    # Unexpected
    "unexpected:search": "An unexpected error occurred during search.",
    "unexpected:dataset": "An unexpected error occurred searching datasets.",
    "unexpected:metadata": "An unexpected error occurred fetching metadata.",
    "unexpected:disaggregation": "An unexpected error occurred fetching disaggregation options.",
    "unexpected:data": "An unexpected error occurred fetching data.",
}


class Data360MCPError(Exception):
    """Base exception for all Data360 MCP errors.

    Attributes:
        error_code: Structured code like "http_error:search".
        detail: Human/LLM-readable error message.
        original_error: The original exception that caused this error, if any.
        log_level: logging level used when this error is constructed. Subclasses
            may override (e.g. NotFoundError uses WARNING).
    """

    log_level: int = logging.ERROR

    def __init__(
        self,
        error_code: str,
        detail: str | None = None,
        original_error: Exception | None = None,
    ):
        self.error_code = error_code
        self.detail = detail or self._get_message(error_code)
        self.original_error = original_error
        super().__init__(self.detail)
        exc_info = (
            (type(original_error), original_error, original_error.__traceback__)
            if original_error is not None
            else None
        )
        _logger.log(
            self.log_level,
            "[%s] %s",
            self.error_code,
            self.detail,
            exc_info=exc_info,
        )

    def _get_message(self, error_code: str) -> str:
        return _ERROR_MESSAGES.get(
            error_code, "Something went wrong. Please try again."
        )

    def to_dict(self) -> dict[str, Any]:
        """Serialize error for structured responses."""
        result: dict[str, Any] = {
            "error_code": self.error_code,
            "detail": self.detail,
        }
        if self.original_error:
            result["original_error"] = str(self.original_error)
        return result


class APIError(Data360MCPError):
    """HTTP errors from the Data360 API (4xx, 5xx responses)."""

    def __init__(
        self,
        context: str,
        status_code: int,
        response_text: str = "",
        original_error: Exception | None = None,
    ):
        self.status_code = status_code
        self.response_text = response_text

        # Sanitize response text - avoid leaking WAF HTML into error messages
        sanitized_response = self._sanitize_error_response(response_text, status_code)
        detail = f"HTTP {status_code}: {sanitized_response}"

        super().__init__(
            error_code=f"http_error:{context}",
            detail=detail,
            original_error=original_error,
        )

    @staticmethod
    def _sanitize_error_response(response_text: str, status_code: int) -> str:
        """Sanitize error responses to avoid leaking HTML/WAF content.

        Args:
            response_text: Raw response body from the backend
            status_code: HTTP status code

        Returns:
            Clean, user-friendly error message
        """
        # Empty or whitespace-only response
        if not response_text or not response_text.strip():
            return _get_status_message(status_code)

        # If response looks like HTML (WAF error pages), don't include it
        # Check both raw and after stripping common whitespace/newlines
        text_to_check = response_text.lstrip()
        if text_to_check.startswith(
            ("<html", "<!DOCTYPE", "<HTML", "<!doctype", "<!Doctype")
        ):
            return _get_status_message(status_code)

        # For JSON error responses, try to extract the error message
        if text_to_check.startswith("{"):
            try:
                import json

                error_data = json.loads(response_text)
                # Common error message fields
                for key in ["error", "message", "detail", "error_description", "code"]:
                    if key in error_data:
                        msg = error_data[key]
                        # Skip if the JSON error field itself contains HTML
                        msg_str = str(msg)
                        if msg_str.lstrip().startswith(
                            ("<html", "<!DOCTYPE", "<HTML", "<!doctype")
                        ):
                            return _get_status_message(status_code)
                        # Limit length to avoid verbose error dumps
                        return (
                            msg_str[:200]
                            if len(msg_str) <= 200
                            else msg_str[:200] + "..."
                        )
            except Exception:
                pass  # Fall through to default message

        # Check if response contains HTML tags anywhere (not just at start)
        # Common WAF patterns: <html, <body, <head, <title
        lower_text = response_text.lower()
        if any(
            tag in lower_text
            for tag in ["<html", "<body", "<head", "<title", "<!doctype"]
        ):
            return _get_status_message(status_code)

        # For other responses, truncate and sanitize
        # Remove excessive whitespace and newlines
        cleaned = " ".join(response_text.split())
        if len(cleaned) > 200:
            return cleaned[:200] + "..."

        return cleaned if cleaned else _get_status_message(status_code)


def _get_status_message(status_code: int) -> str:
    """Get a user-friendly message for common HTTP status codes."""
    status_messages = {
        400: "Bad Request - Invalid parameters",
        401: "Unauthorized - Authentication required",
        403: "Forbidden - Access denied",
        404: "Not Found - Resource does not exist",
        408: "Request Timeout - Session expired",
        413: "Payload Too Large - Request exceeds size limit",
        429: "Too Many Requests - Rate limit exceeded",
        500: "Internal Server Error - Backend service error",
        502: "Bad Gateway - Backend service unavailable",
        503: "Service Unavailable - Backend temporarily down",
        504: "Gateway Timeout - Backend did not respond in time",
    }
    return status_messages.get(
        status_code, f"The request failed with status {status_code}"
    )


class Data360TimeoutError(Data360MCPError):
    """Timeout errors when calling the Data360 API."""

    def __init__(
        self,
        context: str,
        original_error: Exception | None = None,
    ):
        detail = _ERROR_MESSAGES.get(
            f"timeout:{context}", f"Request timed out: {context}"
        )
        super().__init__(
            error_code=f"timeout:{context}",
            detail=detail,
            original_error=original_error,
        )


class RequestError(Data360MCPError):
    """Network-level errors (DNS, connection refused, etc.)."""

    def __init__(
        self,
        context: str,
        original_error: Exception | None = None,
    ):
        detail = _ERROR_MESSAGES.get(
            f"request_error:{context}", f"Request error: {context}"
        )
        super().__init__(
            error_code=f"request_error:{context}",
            detail=detail,
            original_error=original_error,
        )


class ParseError(Data360MCPError):
    """JSON parsing or response validation errors."""

    def __init__(
        self,
        context: str,
        detail: str | None = None,
        original_error: Exception | None = None,
    ):
        detail = detail or _ERROR_MESSAGES.get(
            f"parse_error:{context}", f"Failed to parse response: {context}"
        )
        super().__init__(
            error_code=f"parse_error:{context}",
            detail=detail,
            original_error=original_error,
        )


class ValidationError(Data360MCPError):
    """Invalid input parameters or failed response validation."""

    def __init__(
        self,
        context: str,
        detail: str | None = None,
        original_error: Exception | None = None,
    ):
        detail = detail or _ERROR_MESSAGES.get(
            f"validation_error:{context}", f"Validation error: {context}"
        )
        super().__init__(
            error_code=f"validation_error:{context}",
            detail=detail,
            original_error=original_error,
        )


class NotFoundError(Data360MCPError):
    """Resource not found errors."""

    log_level: int = logging.WARNING

    def __init__(
        self,
        context: str,
        detail: str | None = None,
        original_error: Exception | None = None,
    ):
        detail = detail or _ERROR_MESSAGES.get(
            f"not_found:{context}", f"Not found: {context}"
        )
        super().__init__(
            error_code=f"not_found:{context}",
            detail=detail,
            original_error=original_error,
        )


# ---------------------------------------------------------------------------
# Helper to convert exceptions into Data360MCPError
# ---------------------------------------------------------------------------
def classify_error(exc: Exception, context: str) -> Data360MCPError:
    """Convert an exception into the appropriate Data360MCPError subclass.

    Args:
        exc: The original httpx exception.
        context: The operation context (e.g. "search", "metadata", "data").

    Returns:
        The appropriate Data360MCPError subclass instance.
    """
    import httpx

    if isinstance(exc, httpx.HTTPStatusError):
        return APIError(
            context=context,
            status_code=exc.response.status_code,
            response_text=exc.response.text,
            original_error=exc,
        )
    elif isinstance(exc, httpx.TimeoutException):
        return Data360TimeoutError(context=context, original_error=exc)
    elif isinstance(exc, httpx.RequestError):
        return RequestError(context=context, original_error=exc)
    else:
        return Data360MCPError(
            error_code=f"unexpected:{context}",
            detail=f"Unexpected error: {str(exc)}",
            original_error=exc,
        )