Skip to content

Commit

Permalink
Useful error propagated to user when Marqo receives 429 (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
pandu-k authored Oct 31, 2022
1 parent 55b33a9 commit e30386d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/marqo/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
IndexAlreadyExistsError,
InvalidIndexNameError,
HardwareCompatabilityError,
IndexMaxFieldsError
IndexMaxFieldsError, TooManyRequestsError
)
from urllib3.exceptions import InsecureRequestWarning
import warnings
Expand Down Expand Up @@ -150,6 +150,10 @@ def convert_to_marqo_web_error_and_raise(response: requests.Response, err: reque
response_dict = response.json()
except JSONDecodeError:
raise_catchall_http_as_marqo_error(response=response, err=err)
if response.status_code == 429:
raise TooManyRequestsError(
message="Marqo-OS received too many requests! "
"Please try reducing the frequency of add_documents and update_documents calls.")

try:
open_search_error_type = response_dict["error"]["type"]
Expand Down
5 changes: 5 additions & 0 deletions src/marqo/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def __init__(self, message: str):
error_type = "invalid_request"


class TooManyRequestsError(__InvalidRequestError):
code = "too_many_requests"
status_code = HTTPStatus.TOO_MANY_REQUESTS


class IndexAlreadyExistsError(__InvalidRequestError):
code = "index_already_exists"
status_code = HTTPStatus.CONFLICT
Expand Down
42 changes: 42 additions & 0 deletions tests/tensor_search/test__httprequests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import requests
from tests.marqo_test import MarqoTestCase
from marqo import _httprequests
from unittest import mock
from marqo.tensor_search import tensor_search
from marqo.errors import (
IndexNotFoundError, TooManyRequestsError
)

class Test_HttpRequests(MarqoTestCase):

def setUp(self) -> None:
self.endpoint = self.authorized_url
self.generic_header = {"Content-type": "application/json"}
self.index_name_1 = "my-test-index-1"
try:
tensor_search.delete_index(config=self.config, index_name=self.index_name_1)
except IndexNotFoundError as s:
pass

def test_too_many_reqs_error(self):
mock_post = mock.MagicMock()
mock_response = requests.Response()
mock_response.status_code = 429
mock_response.json = lambda: '{"a":"b"}'
mock_post.return_value = mock_response
tensor_search.create_vector_index(config=self.config, index_name=self.index_name_1)

@mock.patch('requests.post', mock_post)
@mock.patch('marqo._httprequests.ALLOWED_OPERATIONS', {mock_post, requests.get, requests.put})
def run():
try:
res = tensor_search.add_documents(
config=self.config, index_name=self.index_name_1,
docs=[{"some ": "doc"}], auto_refresh=True
)
raise AssertionError
except TooManyRequestsError:
pass
return True
assert run()

0 comments on commit e30386d

Please sign in to comment.