Skip to content

Commit 756597f

Browse files
committed
Clean up exception handling.
1 parent e0550b8 commit 756597f

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

examples/collection_operations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
import os
33
import sys
4+
import typesense
5+
46

57
curr_dir = os.path.dirname(os.path.realpath(__file__))
68
sys.path.insert(1, os.path.abspath(os.path.join(curr_dir, os.pardir)))
79

8-
import typesense
910

1011
client = typesense.Client({
1112
'api_key': 'abcd',

examples/index_and_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
22
import sys
33
import json
4+
import typesense
5+
46

57
curr_dir = os.path.dirname(os.path.realpath(__file__))
68
sys.path.insert(1, os.path.abspath(os.path.join(curr_dir, os.pardir)))
79

8-
import typesense
910

1011
client = typesense.Client({
1112
'api_key': 'abcd',

typesense/api_call.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def __init__(self, config):
2121
for node in self.nodes:
2222
node.last_health_check_ts = int(time.time())
2323

24-
def _check_failed_node(self, node):
24+
@staticmethod
25+
def check_failed_node(node):
2526
current_epoch_ts = int(time.time())
2627
check_node = ((current_epoch_ts - node.last_health_check_ts) > ApiCall.CHECK_FAILED_NODE_INTERVAL_S)
2728
if check_node:
@@ -37,7 +38,7 @@ def get_node(self):
3738
i += 1
3839
self.node_index = (self.node_index + 1) % len(self.nodes)
3940
node = self.nodes[self.node_index]
40-
if node.healthy or self._check_failed_node(node):
41+
if node.healthy or ApiCall.check_failed_node(node):
4142
return node
4243

4344
# None of the nodes are marked healthy, but some of them could have become healthy since last health check.
@@ -65,7 +66,7 @@ def get_exception(http_code):
6566
return TypesenseClientError
6667

6768
# Makes the actual http request, along with retries
68-
def make_request(self, fn, method, endpoint, as_json, **kwargs):
69+
def make_request(self, fn, endpoint, as_json, **kwargs):
6970
num_tries = 0
7071
while num_tries < self.config.num_retries:
7172
num_tries += 1
@@ -79,23 +80,26 @@ def make_request(self, fn, method, endpoint, as_json, **kwargs):
7980

8081
r = fn(url, headers={ApiCall.API_KEY_HEADER_NAME: self.config.api_key}, **kwargs)
8182

83+
# Treat any status code > 0 and < 500 to be an indication that node is healthy
84+
# We exclude 0 since some clients return 0 when request fails
8285
if 0 < r.status_code < 500:
8386
node.healthy = True
8487

85-
if (method != 'post' and r.status_code != 200) or \
86-
(method == 'post' and not (r.status_code == 200 or r.status_code == 201)):
88+
# We should raise a custom exception if status code is not 200 or 201
89+
if r.status_code not in [200, 201]:
8790
error_message = r.json().get('message', 'API error.')
8891
# print('error_message: ' + error_message)
8992
raise ApiCall.get_exception(r.status_code)(r.status_code, error_message)
9093

9194
return r.json() if as_json else r.text
92-
except requests.exceptions.Timeout:
93-
pass
94-
except requests.exceptions.ConnectionError:
95+
except requests.exceptions.Timeout, requests.exceptions.ConnectionError:
96+
# Catch the exception and retry
9597
pass
9698
except TypesenseClientError as typesense_client_error:
99+
# Raise validation exception
97100
raise typesense_client_error
98101
except Exception as e:
102+
# Unknown error fallback
99103
raise e
100104

101105
# print('Failed, retrying after sleep: ' + node.port)
@@ -105,18 +109,18 @@ def make_request(self, fn, method, endpoint, as_json, **kwargs):
105109

106110
def get(self, endpoint, params=None, as_json=True):
107111
params = params or {}
108-
return self.make_request(requests.get, 'get', endpoint, as_json,
112+
return self.make_request(requests.get, endpoint, as_json,
109113
params=params,
110114
timeout=self.config.timeout_seconds)
111115

112116
def post(self, endpoint, body):
113-
return self.make_request(requests.post, 'post', endpoint, True,
117+
return self.make_request(requests.post, endpoint, True,
114118
data=body, timeout=self.config.timeout_seconds)
115119

116120
def put(self, endpoint, body):
117-
return self.make_request(requests.put, 'put', endpoint, True,
121+
return self.make_request(requests.put, endpoint, True,
118122
data=body, timeout=self.config.timeout_seconds)
119123

120124
def delete(self, endpoint):
121-
return self.make_request(requests.delete, 'delete', endpoint, True,
125+
return self.make_request(requests.delete, endpoint, True,
122126
timeout=self.config.timeout_seconds)

0 commit comments

Comments
 (0)