@@ -101,6 +101,28 @@ class GitlabTransferProjectError(GitlabOperationError):
101101 pass
102102
103103
104+ def _raiseErrorFromResponse (response , error ):
105+ """ Tries to parse gitlab error message from response and raises error.
106+
107+ If response status code is 401, raises instead GitlabAuthenticationError.
108+
109+ response: requests response object
110+ error: Error-class to raise. Should be inherited from GitLabError
111+ """
112+
113+ try :
114+ message = response .json ()['message' ]
115+ except (KeyError , ValueError ):
116+ message = response .content
117+
118+ if response .status_code == 401 :
119+ error = GitlabAuthenticationError
120+
121+ raise error (error_message = message ,
122+ response_code = response .status_code ,
123+ response_body = response .content )
124+
125+
104126class Gitlab (object ):
105127 """Represents a GitLab server connection"""
106128 def __init__ (self , url , private_token = None ,
@@ -144,7 +166,7 @@ def credentials_auth(self):
144166 if r .status_code == 201 :
145167 self .user = CurrentUser (self , r .json ())
146168 else :
147- raise GitlabAuthenticationError ( r . json ()[ 'message' ] )
169+ _raiseErrorFromResponse ( r , GitlabAuthenticationError )
148170
149171 self .setToken (self .user .private_token )
150172
@@ -267,10 +289,9 @@ def list(self, obj_class, **kwargs):
267289 del cls_kwargs [key ]
268290
269291 return [cls (self , item , ** cls_kwargs ) for item in r .json () if item is not None ]
270- elif r .status_code == 401 :
271- raise GitlabAuthenticationError (r .json ()['message' ])
272292 else :
273- raise GitlabGetError ('%d: %s' % (r .status_code , r .text ))
293+ _raiseErrorFromResponse (r , GitlabListError )
294+
274295
275296 def get (self , obj_class , id = None , ** kwargs ):
276297 missing = []
@@ -299,12 +320,9 @@ def get(self, obj_class, id=None, **kwargs):
299320
300321 if r .status_code == 200 :
301322 return r .json ()
302- elif r .status_code == 401 :
303- raise GitlabAuthenticationError (r .json ()['message' ])
304- elif r .status_code == 404 :
305- raise GitlabGetError ("Object doesn't exist" )
306323 else :
307- raise GitlabGetError ('%d: %s' % (r .status_code , r .text ))
324+ _raiseErrorFromResponse (r , GitlabGetError )
325+
308326
309327 def delete (self , obj ):
310328 params = obj .__dict__ .copy ()
@@ -335,11 +353,8 @@ def delete(self, obj):
335353
336354 if r .status_code == 200 :
337355 return True
338- elif r .status_code == 401 :
339- raise GitlabAuthenticationError (r .json ()['message' ])
340356 else :
341- raise GitlabDeleteError (r .json ()['message' ])
342- return False
357+ _raiseErrorFromResponse (r , GitlabDeleteError )
343358
344359 def create (self , obj ):
345360 missing = []
@@ -367,10 +382,8 @@ def create(self, obj):
367382
368383 if r .status_code == 201 :
369384 return r .json ()
370- elif r .status_code == 401 :
371- raise GitlabAuthenticationError (r .json ()['message' ])
372385 else :
373- raise GitlabCreateError ( '%d: %s' % ( r . status_code , r . text ) )
386+ _raiseErrorFromResponse ( r , GitlabCreateError )
374387
375388 def update (self , obj ):
376389 missing = []
@@ -402,10 +415,8 @@ def update(self, obj):
402415
403416 if r .status_code == 200 :
404417 return r .json ()
405- elif r .status_code == 401 :
406- raise GitlabAuthenticationError (r .json ()['message' ])
407418 else :
408- raise GitlabUpdateError ( '%d: %s' % ( r . status_code , r . text ) )
419+ _raiseErrorFromResponse ( r , GitlabUpdateError )
409420
410421 def Hook (self , id = None , ** kwargs ):
411422 """Creates/tests/lists system hook(s) known by the GitLab server.
@@ -444,7 +455,7 @@ def UserProject(self, id=None, **kwargs):
444455 def _list_projects (self , url , ** kwargs ):
445456 r = self .rawGet (url , ** kwargs )
446457 if r .status_code != 200 :
447- raise GitlabListError
458+ _raiseErrorFromResponse ( r , GitlabListError )
448459
449460 l = []
450461 for o in r .json ():
@@ -622,7 +633,7 @@ def delete(self):
622633 raise NotImplementedError
623634
624635 if not self ._created :
625- raise GitlabDeleteError
636+ raise GitlabDeleteError ( "Object not yet created" )
626637
627638 return self .gitlab .delete (self )
628639
@@ -783,8 +794,7 @@ def transfer_project(self, id):
783794 url = '/groups/%d/projects/%d' % (self .id , id )
784795 r = self .gitlab .rawPost (url , None )
785796 if r .status_code != 201 :
786- raise GitlabTransferProjectError ()
787-
797+ _raiseErrorFromResponse (r , GitlabTransferProjectError )
788798
789799class Hook (GitlabObject ):
790800 _url = '/hooks'
@@ -826,7 +836,7 @@ def protect(self, protect=True):
826836 else :
827837 del self .protected
828838 else :
829- raise GitlabProtectError
839+ _raiseErrorFromResponse ( r , GitlabProtectError )
830840
831841 def unprotect (self ):
832842 self .protect (False )
@@ -846,8 +856,9 @@ def diff(self):
846856 r = self .gitlab .rawGet (url )
847857 if r .status_code == 200 :
848858 return r .json ()
859+ else :
860+ _raiseErrorFromResponse (r , GitlabGetError )
849861
850- raise GitlabGetError
851862
852863 def blob (self , filepath ):
853864 url = '/projects/%(project_id)s/repository/blobs/%(commit_id)s' % \
@@ -856,8 +867,8 @@ def blob(self, filepath):
856867 r = self .gitlab .rawGet (url )
857868 if r .status_code == 200 :
858869 return r .content
859-
860- raise GitlabGetError
870+ else :
871+ _raiseErrorFromResponse ( r , GitlabGetError )
861872
862873
863874class ProjectKey (GitlabObject ):
@@ -1022,7 +1033,7 @@ def Content(self):
10221033 if r .status_code == 200 :
10231034 return r .content
10241035 else :
1025- raise GitlabGetError
1036+ _raiseErrorFromResponse ( r , GitlabGetError )
10261037
10271038 def Note (self , id = None , ** kwargs ):
10281039 return ProjectSnippetNote ._getListOrObject (self .gitlab , id ,
@@ -1134,17 +1145,17 @@ def tree(self, path='', ref_name=''):
11341145 r = self .gitlab .rawGet (url )
11351146 if r .status_code == 200 :
11361147 return r .json ()
1137-
1138- raise GitlabGetError
1148+ else :
1149+ _raiseErrorFromResponse ( r , GitlabGetError )
11391150
11401151 def blob (self , sha , filepath ):
11411152 url = "%s/%s/repository/blobs/%s" % (self ._url , self .id , sha )
11421153 url += '?filepath=%s' % (filepath )
11431154 r = self .gitlab .rawGet (url )
11441155 if r .status_code == 200 :
11451156 return r .content
1146-
1147- raise GitlabGetError
1157+ else :
1158+ _raiseErrorFromResponse ( r , GitlabGetError )
11481159
11491160 def archive (self , sha = None ):
11501161 url = '/projects/%s/repository/archive' % self .id
@@ -1153,32 +1164,32 @@ def archive(self, sha=None):
11531164 r = self .gitlab .rawGet (url )
11541165 if r .status_code == 200 :
11551166 return r .content
1156-
1157- raise GitlabGetError
1167+ else :
1168+ _raiseErrorFromResponse ( r , GitlabGetError )
11581169
11591170 def create_file (self , path , branch , content , message ):
11601171 url = "/projects/%s/repository/files" % self .id
11611172 url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
11621173 (path , branch , content , message )
11631174 r = self .gitlab .rawPost (url )
11641175 if r .status_code != 201 :
1165- raise GitlabCreateError
1176+ _raiseErrorFromResponse ( r , GitlabCreateError )
11661177
11671178 def update_file (self , path , branch , content , message ):
11681179 url = "/projects/%s/repository/files" % self .id
11691180 url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
11701181 (path , branch , content , message )
11711182 r = self .gitlab .rawPut (url )
11721183 if r .status_code != 200 :
1173- raise GitlabUpdateError
1184+ _raiseErrorFromResponse ( r , GitlabUpdateError )
11741185
11751186 def delete_file (self , path , branch , message ):
11761187 url = "/projects/%s/repository/files" % self .id
11771188 url += "?file_path=%s&branch_name=%s&commit_message=%s" % \
11781189 (path , branch , message )
11791190 r = self .gitlab .rawDelete (url )
11801191 if r .status_code != 200 :
1181- raise GitlabDeleteError
1192+ _raiseErrorFromResponse ( r , GitlabDeleteError )
11821193
11831194
11841195class TeamMember (GitlabObject ):
0 commit comments