File tree Expand file tree Collapse file tree 2 files changed +29
-13
lines changed
Expand file tree Collapse file tree 2 files changed +29
-13
lines changed Original file line number Diff line number Diff line change 1919
2020class BlazarClientException (Exception ):
2121 """Base exception class."""
22- message = _ ("An unknown exception occurred %s." )
22+ message_template = _ ("An unknown exception occurred %s." )
2323 code = 500
2424
2525 def __init__ (self , message = None , ** kwargs ):
26- self .kwargs = kwargs
27-
28- if 'code' not in self .kwargs :
29- try :
30- self .kwargs ['code' ] = self .code
31- except AttributeError :
32- pass
33-
34- if not message :
35- message = self .message % kwargs
36-
37- super (BlazarClientException , self ).__init__ (message )
26+ if 'code' in kwargs :
27+ self .code = kwargs ['code' ]
28+ else :
29+ # ensure code in kwargs to template message
30+ kwargs ["code" ]= self .code
31+ if message :
32+ self .message = message
33+ else :
34+ self .message = self .message_template % kwargs
35+
36+ super (BlazarClientException , self ).__init__ (self .message )
3837
3938
4039class CommandError (BlazarClientException ):
Original file line number Diff line number Diff line change @@ -126,7 +126,24 @@ def test_request_fail_without_body(self, m):
126126 kwargs = {"body" : {"req_key" : "req_value" }}
127127 self .assertRaises (exception .BlazarClientException ,
128128 self .manager .request , url , "POST" , ** kwargs )
129+
130+ @mock .patch ('requests.request' )
131+ def test_request_fail_notfound (self , m ):
132+
133+ m .return_value .status_code = 404
134+ m .return_value .text = "{\" error_code\" : 404, \" error_message\" : \" Object with {'lease_id': 'aaa-bbb-ccc'} not found\" , \" error_name\" : 404}"
135+ url = '/leases/aaa-bbb-ccc'
129136
137+ try :
138+ resp , body = self .manager .get (url )
139+ except exception .BlazarClientException as exc :
140+ self .assertEqual (exc .code , 404 )
141+ print (exc )
142+ else :
143+ # Fail if we don't have the expected exception
144+ # Hack because testtools doesn't support AssertRaises as context Manager
145+ self .assertFalse (True )
146+ m .assert_called_once ()
130147
131148class SessionClientTestCase (tests .TestCase ):
132149
You can’t perform that action at this time.
0 commit comments