Skip to content

Commit 5c44e89

Browse files
authored
HL Python SDK: Add SDK response logging on error (#9471)
* HL Python SDK: Add SDK response logging on error * Fix pytest error
1 parent 63186a0 commit 5c44e89

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

clients/python-wrapper/lakefs/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
from contextlib import contextmanager
77
from typing import Optional, Callable
8+
import logging
89

910
import lakefs_sdk.exceptions
1011
from urllib3 import HTTPResponse
@@ -31,6 +32,8 @@ def __init__(self, status=None, reason=None, body=None):
3132
try: # Try to get message from body
3233
self.body = json.loads(body)
3334
except json.JSONDecodeError:
35+
logging.debug("failed to decode response body: status (%s), reason (%s), body (%s)",
36+
status, reason, body)
3437
self.body = {}
3538
else:
3639
self.body = {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import http
2+
import json
3+
4+
from lakefs_sdk import ApiException
5+
from lakefs.exceptions import api_exception_handler, ServerException
6+
7+
8+
class TestException(ApiException):
9+
__test__ = False # Not a test case
10+
def __init__(self, status: int, reason: str, body: str):
11+
super().__init__()
12+
self.status = status
13+
self.reason = reason
14+
self.body = body
15+
16+
def test_http_error_list():
17+
expected_reason = "my reason"
18+
body = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
19+
resp = TestException(http.HTTPStatus.FORBIDDEN.value, expected_reason, body)
20+
21+
try:
22+
with api_exception_handler():
23+
raise resp
24+
except ServerException as e:
25+
assert expected_reason == e.reason
26+
assert e.body is not None
27+
actual_body = json.dumps(e.body)
28+
assert actual_body == body
29+
return
30+
31+
# make sure exception is not swallowed
32+
assert False, "Exception not caught"
33+
34+
def test_http_error_xml():
35+
expected_reason = ""
36+
body = '''<note>
37+
<to>Tove</to>
38+
<from>Jani</from>
39+
<heading>Reminder</heading>
40+
<body>Don't forget me this weekend!</body>
41+
</note>
42+
'''
43+
resp = TestException(http.HTTPStatus.FORBIDDEN.value, expected_reason, body)
44+
45+
try:
46+
with api_exception_handler():
47+
raise resp
48+
except ServerException as e:
49+
assert expected_reason == e.reason
50+
assert e.body is not None
51+
assert not e.body # Body should be empty
52+
return
53+
54+
# make sure exception is not swallowed
55+
assert False, "Exception not caught"

0 commit comments

Comments
 (0)