Skip to content

Commit ea6e866

Browse files
author
anish
committed
fix - Bug 1207303 - Return code is 0 even if push fails
People use z-p-c in command line scripts and they check return status code if command fails So added exception handler method, that will print error message and exit with return code 1 in case of failure
1 parent 100899b commit ea6e866

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

zanataclient/zanatalib/service.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def __init__(self,*args,**kargs):
2828
setattr(self,key,value)
2929
self.restclient = RestClient(self.base_url)
3030

31+
def excption_handler(self,exception_class,error,error_msg):
32+
try:
33+
raise exception_class(error, error_msg)
34+
except exception_class as e:
35+
print '', e
36+
finally:
37+
sys.exit(1)
38+
3139
def messages(self,res,content,extra_msg=None):
3240
if res['status'] == '200' or res['status'] == '304':
3341
rst = None
@@ -44,30 +52,32 @@ def messages(self,res,content,extra_msg=None):
4452
elif res['status'] == '201':
4553
return True
4654
elif res['status'] == '401':
47-
raise UnAuthorizedException('Error 401',
48-
'This operation is not authorized, please check username and apikey')
55+
self.excption_handler(UnAuthorizedException,
56+
'Error 401','This operation is not authorized, please check username and apikey')
4957
elif res['status'] == '400':
50-
raise BadRequestBodyException('Error 400', content)
58+
self.excption_handler(BadRequestBodyException,
59+
'Error 400',content)
5160
elif res['status'] == '404':
52-
raise UnAvaliableResourceException('Error 404',
53-
'The requested resource/project is not available')
61+
self.excption_handler(UnAvaliableResourceException,
62+
'Error 404','The requested resource/project is not available')
5463
elif res['status'] == '405':
55-
raise NotAllowedException('Error 405',
56-
'The requested method is not allowed')
64+
self.excption_handler(NotAllowedException,
65+
'Error 405','The requested method is not allowed')
5766
elif res['status'] == '409':
58-
raise SameNameDocumentException('Error 409', 'A document with same name already exists.')
67+
self.excption_handler(SameNameDocumentException,
68+
'Error 409','A document with same name already exists')
5969
elif res['status'] == '500':
60-
raise InternalServerError('Error 500', content)
70+
self.excption_handler(InternalServerError,
71+
'Error 500',content)
6172
elif res['status'] == '503':
62-
raise UnavailableServiceError('Error 503',
63-
'Service Temporarily Unavailable, stop processing!')
73+
self.excption_handler(UnavailableServiceError,
74+
'Error 503','Service Temporarily Unavailable, stop processing')
6475
elif res['status'] == '403':
65-
try:
66-
raise ForbiddenException('Error 403', 'You are authenticated but do not have the permission for the requested resource.')
67-
except ForbiddenException as e:
68-
print '', e
69-
finally:
70-
sys.exit(1)
76+
self.excption_handler(ForbiddenException,
77+
'Error 403','You are authenticated but do not have the permission for the requested resource')
7178
else:
72-
raise UnexpectedStatusException('Error',
73-
'Unexpected Status (%s), failed to push: %s' % (res['status'], extra_msg or ""))
79+
self.excption_handler(UnexpectedStatusException,
80+
'Error','Unexpected Status (%s), failed to push: %s' % (res['status'], extra_msg or ""))
81+
82+
83+

0 commit comments

Comments
 (0)