Skip to content

Commit 2337e75

Browse files
committed
Improve error checking on Reductionist response:
- Reductionist will only report 500 internal server errors - here we check JSON body - Any other error response we check text of body - this way we catch the likes of HAProxy connection limits
1 parent 9805a03 commit 2337e75

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

activestorage/reductionist.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def reduce_chunk(session,
8888
print(f"Reductionist request data dictionary: {request_data}")
8989
api_operation = "sum" if operation == "mean" else operation or "select"
9090
url = f'{server}/v2/{api_operation}/'
91-
print("Reductionist Session auth:", session.auth)
9291
response = request(session, url, request_data)
9392

9493
if response.ok:
@@ -253,8 +252,13 @@ def __init__(self, status_code, error):
253252

254253
def decode_and_raise_error(response):
255254
"""Decode an error response and raise ReductionistError."""
256-
try:
257-
error = json.dumps(response.json())
255+
if response.status_code == http.client.INTERNAL_SERVER_ERROR:
256+
try:
257+
error = json.dumps(response.json())
258+
except requests.exceptions.JSONDecodeError as exc:
259+
error = http.client.responses.get(response.status_code, "-")
260+
raise ReductionistError(response.status_code, error) from exc
258261
raise ReductionistError(response.status_code, error)
259-
except requests.exceptions.JSONDecodeError as exc:
260-
raise ReductionistError(response.status_code, "-") from exc
262+
263+
error = http.client.responses.get(response.status_code, "-")
264+
raise ReductionistError(response.status_code, error)

tests/unit/test_reductionist.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,42 @@ def test_reduce_chunk_not_found(mock_request):
299299
operation)
300300

301301
print("Not found exc from reductionist", str(exc.value))
302-
assert str(exc.value) == 'Reductionist error: HTTP 404: -'
302+
assert str(exc.value) == 'Reductionist error: HTTP 404: Not Found'
303+
304+
305+
@mock.patch.object(reductionist, 'request')
306+
def test_reductionist_internal_server_error_json(mock_request):
307+
"""Unit test for Reductionist 500 JSON response."""
308+
response = requests.Response()
309+
response.status_code = 500
310+
response._content = b'{"detail": "backend exploded"}'
311+
response.headers["Content-Type"] = "application/json"
312+
mock_request.return_value = response
313+
314+
active_url = "https://s3.example.com"
315+
access_key = "fake-access"
316+
secret_key = "fake-secret"
317+
cacert = None
318+
s3_url = "https://active.example.com"
319+
offset = 2
320+
size = 128
321+
compression = None
322+
filters = None
323+
missing = []
324+
dtype = np.dtype("int32")
325+
shape = (32, )
326+
axis = (0, )
327+
order = "C"
328+
chunk_selection = [slice(0, 2, 1)]
329+
operation = "min"
330+
331+
session = reductionist.get_session(access_key, secret_key, cacert)
332+
with pytest.raises(reductionist.ReductionistError) as exc:
333+
reductionist.reduce_chunk(session, active_url, s3_url,
334+
offset, size, compression, filters, missing,
335+
dtype, shape, order, chunk_selection, axis,
336+
operation)
337+
338+
assert (
339+
str(exc.value) == 'Reductionist error: HTTP 500: {"detail": "backend exploded"}'
340+
)

0 commit comments

Comments
 (0)