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