Skip to content

Commit

Permalink
fix - Bug 1207303 - Return code is 0 even if push fails
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anish committed Jul 5, 2015
1 parent 100899b commit ea6e866
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions zanataclient/zanatalib/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def __init__(self,*args,**kargs):
setattr(self,key,value)
self.restclient = RestClient(self.base_url)

def excption_handler(self,exception_class,error,error_msg):
try:
raise exception_class(error, error_msg)
except exception_class as e:
print '', e
finally:
sys.exit(1)

def messages(self,res,content,extra_msg=None):
if res['status'] == '200' or res['status'] == '304':
rst = None
Expand All @@ -44,30 +52,32 @@ def messages(self,res,content,extra_msg=None):
elif res['status'] == '201':
return True
elif res['status'] == '401':
raise UnAuthorizedException('Error 401',
'This operation is not authorized, please check username and apikey')
self.excption_handler(UnAuthorizedException,
'Error 401','This operation is not authorized, please check username and apikey')
elif res['status'] == '400':
raise BadRequestBodyException('Error 400', content)
self.excption_handler(BadRequestBodyException,
'Error 400',content)
elif res['status'] == '404':
raise UnAvaliableResourceException('Error 404',
'The requested resource/project is not available')
self.excption_handler(UnAvaliableResourceException,
'Error 404','The requested resource/project is not available')
elif res['status'] == '405':
raise NotAllowedException('Error 405',
'The requested method is not allowed')
self.excption_handler(NotAllowedException,
'Error 405','The requested method is not allowed')
elif res['status'] == '409':
raise SameNameDocumentException('Error 409', 'A document with same name already exists.')
self.excption_handler(SameNameDocumentException,
'Error 409','A document with same name already exists')
elif res['status'] == '500':
raise InternalServerError('Error 500', content)
self.excption_handler(InternalServerError,
'Error 500',content)
elif res['status'] == '503':
raise UnavailableServiceError('Error 503',
'Service Temporarily Unavailable, stop processing!')
self.excption_handler(UnavailableServiceError,
'Error 503','Service Temporarily Unavailable, stop processing')
elif res['status'] == '403':
try:
raise ForbiddenException('Error 403', 'You are authenticated but do not have the permission for the requested resource.')
except ForbiddenException as e:
print '', e
finally:
sys.exit(1)
self.excption_handler(ForbiddenException,
'Error 403','You are authenticated but do not have the permission for the requested resource')
else:
raise UnexpectedStatusException('Error',
'Unexpected Status (%s), failed to push: %s' % (res['status'], extra_msg or ""))
self.excption_handler(UnexpectedStatusException,
'Error','Unexpected Status (%s), failed to push: %s' % (res['status'], extra_msg or ""))



0 comments on commit ea6e866

Please sign in to comment.