From 31494d14e16e8e2b83019b740da7e8240d60a468 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Thu, 4 Dec 2025 19:57:07 +0000 Subject: [PATCH] Fixing errors --- trustgraph-base/trustgraph/api/__init__.py | 38 ++++- trustgraph-base/trustgraph/api/exceptions.py | 130 +++++++++++++++++- .../trustgraph/api/socket_client.py | 12 +- 3 files changed, 173 insertions(+), 7 deletions(-) diff --git a/trustgraph-base/trustgraph/api/__init__.py b/trustgraph-base/trustgraph/api/__init__.py index 7a3fc86d..0ecb760e 100644 --- a/trustgraph-base/trustgraph/api/__init__.py +++ b/trustgraph-base/trustgraph/api/__init__.py @@ -34,7 +34,26 @@ from .types import ( ) # Exceptions -from .exceptions import ProtocolException, ApplicationException +from .exceptions import ( + ProtocolException, + TrustGraphException, + AgentError, + ConfigError, + DocumentRagError, + FlowError, + GatewayError, + GraphRagError, + LLMError, + LoadError, + LookupError, + NLPQueryError, + ObjectsQueryError, + RequestError, + StructuredQueryError, + UnexpectedError, + # Legacy alias + ApplicationException, +) __all__ = [ # Core API @@ -75,6 +94,21 @@ __all__ = [ # Exceptions "ProtocolException", - "ApplicationException", + "TrustGraphException", + "AgentError", + "ConfigError", + "DocumentRagError", + "FlowError", + "GatewayError", + "GraphRagError", + "LLMError", + "LoadError", + "LookupError", + "NLPQueryError", + "ObjectsQueryError", + "RequestError", + "StructuredQueryError", + "UnexpectedError", + "ApplicationException", # Legacy alias ] diff --git a/trustgraph-base/trustgraph/api/exceptions.py b/trustgraph-base/trustgraph/api/exceptions.py index b3f732d4..311d2651 100644 --- a/trustgraph-base/trustgraph/api/exceptions.py +++ b/trustgraph-base/trustgraph/api/exceptions.py @@ -1,6 +1,134 @@ +""" +TrustGraph API Exceptions +Exception hierarchy for errors returned by TrustGraph services. +Each service error type maps to a specific exception class. +""" + +# Protocol-level exceptions (communication errors) class ProtocolException(Exception): + """Raised when WebSocket protocol errors occur""" pass -class ApplicationException(Exception): + +# Base class for all TrustGraph application errors +class TrustGraphException(Exception): + """Base class for all TrustGraph service errors""" + def __init__(self, message: str, error_type: str = None): + super().__init__(message) + self.message = message + self.error_type = error_type + + +# Service-specific exceptions +class AgentError(TrustGraphException): + """Agent service error""" pass + + +class ConfigError(TrustGraphException): + """Configuration service error""" + pass + + +class DocumentRagError(TrustGraphException): + """Document RAG retrieval error""" + pass + + +class FlowError(TrustGraphException): + """Flow management error""" + pass + + +class GatewayError(TrustGraphException): + """API Gateway error""" + pass + + +class GraphRagError(TrustGraphException): + """Graph RAG retrieval error""" + pass + + +class LLMError(TrustGraphException): + """LLM service error""" + pass + + +class LoadError(TrustGraphException): + """Data loading error""" + pass + + +class LookupError(TrustGraphException): + """Lookup/search error""" + pass + + +class NLPQueryError(TrustGraphException): + """NLP query service error""" + pass + + +class ObjectsQueryError(TrustGraphException): + """Objects query service error""" + pass + + +class RequestError(TrustGraphException): + """Request processing error""" + pass + + +class StructuredQueryError(TrustGraphException): + """Structured query service error""" + pass + + +class UnexpectedError(TrustGraphException): + """Unexpected/unknown error""" + pass + + +# Mapping from error type string to exception class +ERROR_TYPE_MAPPING = { + "agent-error": AgentError, + "config-error": ConfigError, + "document-rag-error": DocumentRagError, + "flow-error": FlowError, + "gateway-error": GatewayError, + "graph-rag-error": GraphRagError, + "llm-error": LLMError, + "load-error": LoadError, + "lookup-error": LookupError, + "nlp-query-error": NLPQueryError, + "objects-query-error": ObjectsQueryError, + "request-error": RequestError, + "structured-query-error": StructuredQueryError, + "unexpected-error": UnexpectedError, +} + + +def raise_from_error_dict(error_dict: dict) -> None: + """ + Raise appropriate exception from TrustGraph error dictionary. + + Args: + error_dict: Dictionary with 'type' and 'message' keys + + Raises: + Appropriate TrustGraphException subclass based on error type + """ + error_type = error_dict.get("type", "unexpected-error") + message = error_dict.get("message", "Unknown error") + + # Look up exception class, default to UnexpectedError + exception_class = ERROR_TYPE_MAPPING.get(error_type, UnexpectedError) + + # Raise the appropriate exception + raise exception_class(message, error_type) + + +# Legacy exception for backwards compatibility +ApplicationException = TrustGraphException diff --git a/trustgraph-base/trustgraph/api/socket_client.py b/trustgraph-base/trustgraph/api/socket_client.py index b758f230..6297025f 100644 --- a/trustgraph-base/trustgraph/api/socket_client.py +++ b/trustgraph-base/trustgraph/api/socket_client.py @@ -6,7 +6,7 @@ from typing import Optional, Dict, Any, Iterator, Union, List from threading import Lock from . types import AgentThought, AgentObservation, AgentAnswer, RAGChunk, StreamingChunk -from . exceptions import ProtocolException, ApplicationException +from . exceptions import ProtocolException, raise_from_error_dict class SocketClient: @@ -126,7 +126,7 @@ class SocketClient: raise ProtocolException(f"Response ID mismatch") if "error" in response: - raise ApplicationException(response["error"]) + raise_from_error_dict(response["error"]) if "response" not in response: raise ProtocolException(f"Missing response in message") @@ -171,11 +171,15 @@ class SocketClient: continue # Ignore messages for other requests if "error" in response: - raise ApplicationException(response["error"]) + raise_from_error_dict(response["error"]) if "response" in response: resp = response["response"] + # Check for errors in response chunks + if "error" in resp: + raise_from_error_dict(resp["error"]) + # Parse different chunk types chunk = self._parse_chunk(resp) yield chunk @@ -211,7 +215,7 @@ class SocketClient: return RAGChunk( content=content, end_of_stream=resp.get("end_of_stream", False), - error=resp.get("error") + error=None # Errors are always thrown, never stored ) def close(self) -> None: