|
| 1 | +"""Data Fabric query engine error classification. |
| 2 | +
|
| 3 | +Maps error codes from the DF query engine invoking the "query_execute" endpoint to actionable |
| 4 | +categories so that callers (e.g. the agent SQL sub-graph) can decide |
| 5 | +whether to retry, ask the LLM to fix the SQL, or surface an infra error. |
| 6 | +
|
| 7 | +The server error response JSON has the shape: |
| 8 | + {"error": "<message>", "code": "<ERROR_CODE>", "traceId": "<uuid>"} |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from dataclasses import dataclass |
| 14 | +from typing import TYPE_CHECKING, Any, Callable, TypeVar |
| 15 | + |
| 16 | +from ._extractors._helpers import extract_service_prefix |
| 17 | +from .datafabric_error_codes import ( |
| 18 | + _QUERY_ENTITY_RECORDS_ERROR_CODES, |
| 19 | + DataFabricErrorCategory, |
| 20 | + classify_error_code, |
| 21 | +) |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from ._enriched_exception import EnrichedException |
| 25 | + |
| 26 | + |
| 27 | +TCallable = TypeVar("TCallable", bound=Callable[..., Any]) |
| 28 | + |
| 29 | + |
| 30 | +def attach_datafabric_error_mapping( |
| 31 | + method_name: str, |
| 32 | +) -> Callable[[TCallable], TCallable]: |
| 33 | + """Attach Data Fabric error metadata to a query method.""" |
| 34 | + |
| 35 | + def decorator(func: TCallable) -> TCallable: |
| 36 | + func.__uipath_datafabric_method__ = method_name # type: ignore[attr-defined] |
| 37 | + func.__uipath_datafabric_error_codes__ = ( # type: ignore[attr-defined] |
| 38 | + _QUERY_ENTITY_RECORDS_ERROR_CODES |
| 39 | + ) |
| 40 | + return func |
| 41 | + |
| 42 | + return decorator |
| 43 | + |
| 44 | + |
| 45 | +@dataclass(frozen=True) |
| 46 | +class DataFabricError: |
| 47 | + """Structured error parsed from a DF query engine response.""" |
| 48 | + |
| 49 | + code: str | None |
| 50 | + message: str | None |
| 51 | + trace_id: str | None |
| 52 | + category: DataFabricErrorCategory |
| 53 | + |
| 54 | + @property |
| 55 | + def is_retryable(self) -> bool: |
| 56 | + return self.category == DataFabricErrorCategory.RETRYABLE |
| 57 | + |
| 58 | + @property |
| 59 | + def is_bad_sql(self) -> bool: |
| 60 | + return self.category == DataFabricErrorCategory.BAD_SQL |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def from_enriched_exception(exc: EnrichedException) -> DataFabricError | None: |
| 64 | + """Extract a DataFabricError from an EnrichedException, if applicable. |
| 65 | +
|
| 66 | + Returns None if the exception is not from a Data Fabric endpoint. |
| 67 | + """ |
| 68 | + if extract_service_prefix(exc.url) != "datafabric_": |
| 69 | + return None |
| 70 | + |
| 71 | + info = exc.error_info |
| 72 | + code = info.error_code if info else None |
| 73 | + message = info.message if info else None |
| 74 | + trace_id = info.trace_id if info else None |
| 75 | + |
| 76 | + return DataFabricError( |
| 77 | + code=code, |
| 78 | + message=message, |
| 79 | + trace_id=trace_id, |
| 80 | + category=classify_error_code(code), |
| 81 | + ) |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def from_response_body(body: dict[str, Any]) -> DataFabricError: |
| 85 | + """Parse a DataFabricError directly from a response body dict.""" |
| 86 | + raw_code = body.get("code") |
| 87 | + code = ( |
| 88 | + str(raw_code) |
| 89 | + if raw_code is not None and not isinstance(raw_code, (dict, list)) |
| 90 | + else None |
| 91 | + ) |
| 92 | + message = body.get("error") |
| 93 | + if not isinstance(message, str): |
| 94 | + message = ( |
| 95 | + body.get("message") if isinstance(body.get("message"), str) else None |
| 96 | + ) |
| 97 | + trace_id = body.get("traceId") |
| 98 | + if not isinstance(trace_id, str): |
| 99 | + trace_id = ( |
| 100 | + body.get("requestId") |
| 101 | + if isinstance(body.get("requestId"), str) |
| 102 | + else None |
| 103 | + ) |
| 104 | + return DataFabricError( |
| 105 | + code=code, |
| 106 | + message=message, |
| 107 | + trace_id=trace_id, |
| 108 | + category=classify_error_code(code), |
| 109 | + ) |
0 commit comments