Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NotFoundError to simplify exception handling #24

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_vectors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import uuid
import turbopuffer as tpuf
import tests
import pytest
from datetime import datetime


Expand Down Expand Up @@ -485,3 +486,12 @@ def test_attribute_types():
]],
)
assert len(results) == 1

def test_not_found_error():
ns = tpuf.Namespace(tests.test_prefix + 'not_found')

with pytest.raises(tpuf.NotFoundError):
ns.query(
top_k=5,
vector=[0.0, 0.0],
)
2 changes: 1 addition & 1 deletion turbopuffer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def dump_json_bytes(obj): return json.dumps(obj, cls=NumpyEncoder).encode()
from turbopuffer.namespace import Namespace, namespaces
from turbopuffer.vectors import VectorColumns, VectorRow, VectorResult
from turbopuffer.query import VectorQuery, Filters
from turbopuffer.error import TurbopufferError, AuthenticationError, APIError
from turbopuffer.error import TurbopufferError, AuthenticationError, APIError, NotFoundError
10 changes: 5 additions & 5 deletions turbopuffer/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
import turbopuffer as tpuf
import gzip
from turbopuffer.error import AuthenticationError, APIError
from turbopuffer.error import AuthenticationError, raise_api_error
from typing import Optional, List


Expand Down Expand Up @@ -121,7 +121,7 @@ def make_api_request(self,
try:
content = response.json()
except json.JSONDecodeError as err:
raise APIError(response.status_code, traceback.format_exception_only(err), response.text)
raise_api_error(response.status_code, traceback.format_exception_only(err), response.text)

if response.ok:
performance['total_time'] = time.monotonic() - start
Expand All @@ -130,9 +130,9 @@ def make_api_request(self,
'performance': performance,
})
else:
raise APIError(response.status_code, content.get('status', 'error'), content.get('error', ''))
raise_api_error(response.status_code, content.get('status', 'error'), content.get('error', ''))
else:
raise APIError(response.status_code, 'Server returned non-JSON response', response.text)
raise_api_error(response.status_code, 'Server returned non-JSON response', response.text)
except requests.HTTPError as http_err:
retry_attempt += 1
# print(traceback.format_exc())
Expand All @@ -141,6 +141,6 @@ def make_api_request(self,
time.sleep(2 ** retry_attempt) # exponential falloff up to 64 seconds for 6 retries.
else:
print(f'Request failed after {retry_attempt} attempts...')
raise APIError(http_err.response.status_code,
raise_api_error(http_err.response.status_code,
f'Request to {http_err.request.url} failed after {retry_attempt} attempts',
str(http_err))
9 changes: 9 additions & 0 deletions turbopuffer/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ def __init__(self, status_code: int, status_name: str, message: str):
self.status_code = status_code
self.status_name = status_name
super().__init__(f'{status_name} (HTTP {status_code}): {message}')

class NotFoundError(APIError):
pass

def raise_api_error(status_code, status_name, message):
if status_code == 404:
raise NotFoundError(status_code, status_name, message)
else:
raise APIError(status_code, status_name, message)
Comment on lines +18 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful

Loading