Skip to content

Commit

Permalink
CV2-4851 add generic error capturing logic for any failed jobs during…
Browse files Browse the repository at this point in the history
… fingerprinting
  • Loading branch information
DGaffney committed Jul 1, 2024
1 parent c8f4a49 commit a577341
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def get_response(self, message: schemas.Message) -> schemas.GenericItem:
"""
result = Cache.get_cached_result(message.body.content_hash)
if not result:
result = self.process(message)
Cache.set_cached_result(message.body.content_hash, result)
try:
result = self.process(message)
Cache.set_cached_result(message.body.content_hash, result)
except Exception as e:
return schemas.ErrorResponse(error=str(e), error_details={"exception": str(e)})
return result

def respond(self, messages: Union[List[schemas.Message], schemas.Message]) -> List[schemas.Message]:
Expand Down
5 changes: 4 additions & 1 deletion lib/schemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from pydantic import BaseModel, ValidationError
from typing import Any, Dict, List, Optional, Union
class ErrorResponse(BaseModel):
error: Optional[str] = None
error_details: Optional[Dict] = None

class MediaResponse(BaseModel):
hash_value: Optional[Any] = None
Expand All @@ -19,7 +22,7 @@ class GenericItem(BaseModel):
text: Optional[str] = None
raw: Optional[Dict] = {}
parameters: Optional[Dict] = {}
result: Optional[Union[MediaResponse, VideoResponse, YakeKeywordsResponse]] = None
result: Optional[Union[ErrorResponse, MediaResponse, VideoResponse, YakeKeywordsResponse]] = None

class Message(BaseModel):
body: GenericItem
Expand Down
22 changes: 22 additions & 0 deletions test/lib/queue/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lib import schemas
from test.lib.queue.fake_sqs_message import FakeSQSMessage
from concurrent.futures import TimeoutError

class TestModelTimeout:
def __init__(self):
self.model_name = "timeout.TestModelTimeout"
Expand Down Expand Up @@ -227,5 +228,26 @@ def test_delete_processed_messages(self, mock_delete_messages):
self.queue.delete_processed_messages(messages_with_queues)
mock_delete_messages.assert_called_once_with(messages_with_queues)

def test_error_capturing_in_get_response(self):
"""
Test that get_response captures errors and returns an ErrorResponse.
"""
message_data = {
"body": {"id": 1, "callback_url": "http://example.com", "text": "This is a test"},
"model_name": "generic"
}
message = schemas.parse_message(message_data)
message.body.content_hash = "test_hash"

# Simulate an error in the process method
self.model.process = MagicMock(side_effect=Exception("Test error"))

result = self.model.get_response(message)

self.assertIsInstance(result, schemas.ErrorResponse)
self.assertEqual(result.error, "Test error")
self.assertIn("exception", result.error_details)
self.assertEqual(result.error_details["exception"], "Test error")

if __name__ == '__main__':
unittest.main()

0 comments on commit a577341

Please sign in to comment.