Skip to content

Commit 70f00cb

Browse files
committed
Add try-except block around potential failure
1 parent 02dee09 commit 70f00cb

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

backend/onyx/connectors/teams/connector.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
from onyx.connectors.interfaces import SecondsSinceUnixEpoch
2626
from onyx.connectors.models import BasicExpertInfo
2727
from onyx.connectors.models import ConnectorCheckpoint
28+
from onyx.connectors.models import ConnectorFailure
2829
from onyx.connectors.models import ConnectorMissingCredentialError
2930
from onyx.connectors.models import Document
31+
from onyx.connectors.models import EntityFailure
3032
from onyx.connectors.models import TextSection
3133
from onyx.file_processing.html_utils import parse_html_page_basic
3234
from onyx.utils.logger import setup_logger
@@ -191,7 +193,7 @@ def load_from_checkpoint(
191193
)
192194

193195
docs = [
194-
_collect_document_for_channel_id(
196+
_collect_documents_for_channel(
195197
graph_client=self.graph_client,
196198
team=team,
197199
channel=channel,
@@ -458,18 +460,17 @@ def _collect_all_channels_from_team(
458460
return channels
459461

460462

461-
def _collect_document_for_channel_id(
463+
def _collect_documents_for_channel(
462464
graph_client: GraphClient,
463465
team: Team,
464466
channel: Channel,
465467
start: SecondsSinceUnixEpoch,
466468
end: SecondsSinceUnixEpoch,
467-
) -> Iterator[Document | None]:
469+
) -> Iterator[Document | None | ConnectorFailure]:
468470
"""
469-
This function yields just one singular `Document`.
471+
This function yields an iterator of `Document`s, where each `Document` corresponds to a "thread".
470472
471-
The reason why this function returns an instance of `Iterator` is because
472-
that is what `parallel_yield` expects. We want this to be lazily evaluated.
473+
A "thread" is the conjunction of the "root" message and all of its replies.
473474
"""
474475

475476
# Server-side filter conditions are not supported on the chat-messages API.
@@ -487,18 +488,27 @@ def _collect_document_for_channel_id(
487488
if not _should_process_message(message=message, start=start, end=end):
488489
continue
489490

490-
replies = list(message.replies.get_all().execute_query())
491-
thread = [message]
492-
thread.extend(replies[::-1])
493-
494-
# Note:
495-
# We convert an entire *thread* (including the root message and its replies) into one, singular `Document`.
496-
# I.e., we don't convert each individual message and each individual reply into their own individual `Document`s.
497-
if doc := _convert_thread_to_document(
498-
channel=channel,
499-
thread=thread,
500-
):
501-
yield doc
491+
try:
492+
replies = list(message.replies.get_all().execute_query())
493+
thread = [message]
494+
thread.extend(replies[::-1])
495+
496+
# Note:
497+
# We convert an entire *thread* (including the root message and its replies) into one, singular `Document`.
498+
# I.e., we don't convert each individual message and each individual reply into their own individual `Document`s.
499+
if doc := _convert_thread_to_document(
500+
channel=channel,
501+
thread=thread,
502+
):
503+
yield doc
504+
except Exception as e:
505+
yield ConnectorFailure(
506+
failed_entity=EntityFailure(
507+
entity_id="messages",
508+
),
509+
failure_message=f"Retrieval of message and its replies failed; {channel.id=} {message.id}",
510+
exception=e,
511+
)
502512

503513

504514
def _should_process_message(

0 commit comments

Comments
 (0)