fix: Simplify exception handling and refactor error messaging (#4496)

* Refactor `ExceptionWithMessageError` to remove redundant exception parameter

* Handle nested exceptions in message schema initialization

* fix: Simplify exception handling in LCAgentComponent
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-11-11 13:10:58 -03:00 • committed by GitHub
commit 11888121af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 5 deletions

View file

@ -160,7 +160,7 @@ class LCAgentComponent(Component):
msg_id = e.agent_message.id msg_id = e.agent_message.id
await asyncio.to_thread(delete_message, id_=msg_id) await asyncio.to_thread(delete_message, id_=msg_id)
self._send_message_event(e.agent_message, category="remove_message") self._send_message_event(e.agent_message, category="remove_message")
raise e.exception # noqa: B904 raise
except Exception: except Exception:
raise raise

View file

@ -14,9 +14,8 @@ from langflow.schema.message import Message
class ExceptionWithMessageError(Exception): class ExceptionWithMessageError(Exception):
def __init__(self, e: Exception, agent_message: Message): def __init__(self, agent_message: Message):
self.agent_message = agent_message self.agent_message = agent_message
self.exception = e
super().__init__() super().__init__()
@ -253,5 +252,5 @@ async def process_agent_events(
start_time = start_time or perf_counter() start_time = start_time or perf_counter()
agent_message.properties.state = "complete" agent_message.properties.state = "complete"
except Exception as e: except Exception as e:
raise ExceptionWithMessageError(e, agent_message) from e raise ExceptionWithMessageError(agent_message) from e
return Message(**agent_message.model_dump()) return Message(**agent_message.model_dump())

View file

@ -339,12 +339,15 @@ class ErrorMessage(Message):
def __init__( def __init__(
self, self,
exception: Exception, exception: BaseException,
session_id: str, session_id: str,
source: Source, source: Source,
trace_name: str | None = None, trace_name: str | None = None,
flow_id: str | None = None, flow_id: str | None = None,
) -> None: ) -> None:
# This is done to avoid circular imports
if exception.__class__.__name__ == "ExceptionWithMessageError" and exception.__cause__ is not None:
exception = exception.__cause__
# Get the error reason # Get the error reason
reason = f"**{exception.__class__.__name__}**\n" reason = f"**{exception.__class__.__name__}**\n"
if hasattr(exception, "body") and "message" in exception.body: if hasattr(exception, "body") and "message" in exception.body: