Revision f4de4c91
b/kamaki/clients/__init__.py | ||
---|---|---|
51 | 51 |
try: |
52 | 52 |
message += '' if message and message[-1] == '\n' else '\n' |
53 | 53 |
serv_stat, sep, new_msg = message.partition('{') |
54 |
new_msg = sep + new_msg |
|
54 |
new_msg = sep + new_msg[:-1 if new_msg.endswith('\n') else 0]
|
|
55 | 55 |
json_msg = loads(new_msg) |
56 | 56 |
key = json_msg.keys()[0] |
57 |
serv_stat = serv_stat.strip() |
|
57 | 58 |
|
58 | 59 |
json_msg = json_msg[key] |
59 |
message = '%s %s (%s)\n' % (serv_stat, key, json_msg['message'])\ |
|
60 |
if 'message' in json_msg else '%s %s' % (serv_stat, key) |
|
61 |
if 'code' in json_msg: |
|
62 |
status = json_msg['code'] |
|
60 |
message = '%s %s (%s)\n' % ( |
|
61 |
serv_stat, |
|
62 |
key, |
|
63 |
json_msg['message']) if ( |
|
64 |
'message' in json_msg) else '%s %s' % (serv_stat, key) |
|
65 |
status = json_msg.get('code', status) |
|
63 | 66 |
if 'details' in json_msg: |
64 | 67 |
if not details: |
65 | 68 |
details = [] |
66 |
elif not isinstance(details, list):
|
|
69 |
if not isinstance(details, list): |
|
67 | 70 |
details = [details] |
68 | 71 |
if json_msg['details']: |
69 | 72 |
details.append(json_msg['details']) |
70 |
except: |
|
73 |
except Exception:
|
|
71 | 74 |
pass |
72 |
|
|
73 |
super(ClientError, self).__init__(message) |
|
74 |
self.status = status
|
|
75 |
self.details = details if details else [] |
|
75 |
finally: |
|
76 |
super(ClientError, self).__init__(message)
|
|
77 |
self.status = status if isinstance(status, int) else 0
|
|
78 |
self.details = details if details else []
|
|
76 | 79 |
|
77 | 80 |
|
78 | 81 |
class SilentEvent(Thread): |
b/kamaki/clients/test.py | ||
---|---|---|
34 | 34 |
from unittest import makeSuite, TestSuite, TextTestRunner, TestCase |
35 | 35 |
from time import sleep |
36 | 36 |
from inspect import getmembers, isclass |
37 |
from json import loads |
|
37 | 38 |
|
38 | 39 |
from kamaki.clients.connection.test import ( |
39 | 40 |
KamakiConnection, |
... | ... | |
49 | 50 |
from kamaki.clients.pithos.test import Pithos, PithosRest |
50 | 51 |
|
51 | 52 |
|
52 |
class SilentEvent(TestCase): |
|
53 |
class ClientError(TestCase): |
|
54 |
|
|
55 |
def test___init__(self): |
|
56 |
from kamaki.clients import ClientError |
|
57 |
for msg, status, details, exp_msg, exp_status, exp_details in ( |
|
58 |
('some msg', 42, 0.28, 0, 0, 0), |
|
59 |
('some msg', 'fail', [], 0, 0, 0), |
|
60 |
('some msg', 42, 'details on error', 0, 0, 0), |
|
61 |
( |
|
62 |
'404 {"ExampleError":' |
|
63 |
' {"message": "a msg", "code": 42, "details": "dets"}}', |
|
64 |
404, |
|
65 |
0, |
|
66 |
'404 ExampleError (a msg)\n', |
|
67 |
42, |
|
68 |
['dets']), |
|
69 |
( |
|
70 |
'404 {"ExampleError":' |
|
71 |
' {"message": "a msg", "code": 42}}', |
|
72 |
404, |
|
73 |
'details on error', |
|
74 |
'404 ExampleError (a msg)\n', |
|
75 |
42, |
|
76 |
0), |
|
77 |
( |
|
78 |
'404 {"ExampleError":' |
|
79 |
' {"details": "Explain your error"}}', |
|
80 |
404, |
|
81 |
'details on error', |
|
82 |
'404 ExampleError', |
|
83 |
0, |
|
84 |
['details on error', 'Explain your error']), |
|
85 |
('some msg\n', -10, ['details', 'on', 'error'], 0, 0, 0)): |
|
86 |
ce = ClientError(msg, status, details) |
|
87 |
exp_msg = exp_msg or (msg if msg.endswith('\n') else msg + '\n') |
|
88 |
exp_status = exp_status or status |
|
89 |
exp_details = exp_details or details |
|
90 |
self.assertEqual('%s' % ce, exp_msg) |
|
91 |
self.assertEqual( |
|
92 |
exp_status if isinstance(exp_status, int) else 0, |
|
93 |
ce.status) |
|
94 |
self.assertEqual(exp_details, ce.details) |
|
95 |
|
|
53 | 96 |
|
54 |
can_finish = -1
|
|
97 |
class SilentEvent(TestCase):
|
|
55 | 98 |
|
56 | 99 |
def thread_content(self, methodid): |
57 | 100 |
wait = 0.1 |
101 |
self.can_finish = -1 |
|
58 | 102 |
while self.can_finish < methodid and wait < 4: |
59 | 103 |
sleep(wait) |
60 | 104 |
wait = 2 * wait |
Also available in: Unified diff