@@ -101,6 +101,28 @@ class GitlabTransferProjectError(GitlabOperationError):
101
101
pass
102
102
103
103
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
+
104
126
class Gitlab (object ):
105
127
"""Represents a GitLab server connection"""
106
128
def __init__ (self , url , private_token = None ,
@@ -144,7 +166,7 @@ def credentials_auth(self):
144
166
if r .status_code == 201 :
145
167
self .user = CurrentUser (self , r .json ())
146
168
else :
147
- raise GitlabAuthenticationError ( r . json ()[ 'message' ] )
169
+ _raiseErrorFromResponse ( r , GitlabAuthenticationError )
148
170
149
171
self .setToken (self .user .private_token )
150
172
@@ -267,10 +289,9 @@ def list(self, obj_class, **kwargs):
267
289
del cls_kwargs [key ]
268
290
269
291
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' ])
272
292
else :
273
- raise GitlabGetError ('%d: %s' % (r .status_code , r .text ))
293
+ _raiseErrorFromResponse (r , GitlabListError )
294
+
274
295
275
296
def get (self , obj_class , id = None , ** kwargs ):
276
297
missing = []
@@ -299,12 +320,9 @@ def get(self, obj_class, id=None, **kwargs):
299
320
300
321
if r .status_code == 200 :
301
322
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" )
306
323
else :
307
- raise GitlabGetError ('%d: %s' % (r .status_code , r .text ))
324
+ _raiseErrorFromResponse (r , GitlabGetError )
325
+
308
326
309
327
def delete (self , obj ):
310
328
params = obj .__dict__ .copy ()
@@ -335,11 +353,8 @@ def delete(self, obj):
335
353
336
354
if r .status_code == 200 :
337
355
return True
338
- elif r .status_code == 401 :
339
- raise GitlabAuthenticationError (r .json ()['message' ])
340
356
else :
341
- raise GitlabDeleteError (r .json ()['message' ])
342
- return False
357
+ _raiseErrorFromResponse (r , GitlabDeleteError )
343
358
344
359
def create (self , obj ):
345
360
missing = []
@@ -367,10 +382,8 @@ def create(self, obj):
367
382
368
383
if r .status_code == 201 :
369
384
return r .json ()
370
- elif r .status_code == 401 :
371
- raise GitlabAuthenticationError (r .json ()['message' ])
372
385
else :
373
- raise GitlabCreateError ( '%d: %s' % ( r . status_code , r . text ) )
386
+ _raiseErrorFromResponse ( r , GitlabCreateError )
374
387
375
388
def update (self , obj ):
376
389
missing = []
@@ -402,10 +415,8 @@ def update(self, obj):
402
415
403
416
if r .status_code == 200 :
404
417
return r .json ()
405
- elif r .status_code == 401 :
406
- raise GitlabAuthenticationError (r .json ()['message' ])
407
418
else :
408
- raise GitlabUpdateError ( '%d: %s' % ( r . status_code , r . text ) )
419
+ _raiseErrorFromResponse ( r , GitlabUpdateError )
409
420
410
421
def Hook (self , id = None , ** kwargs ):
411
422
"""Creates/tests/lists system hook(s) known by the GitLab server.
@@ -444,7 +455,7 @@ def UserProject(self, id=None, **kwargs):
444
455
def _list_projects (self , url , ** kwargs ):
445
456
r = self .rawGet (url , ** kwargs )
446
457
if r .status_code != 200 :
447
- raise GitlabListError
458
+ _raiseErrorFromResponse ( r , GitlabListError )
448
459
449
460
l = []
450
461
for o in r .json ():
@@ -622,7 +633,7 @@ def delete(self):
622
633
raise NotImplementedError
623
634
624
635
if not self ._created :
625
- raise GitlabDeleteError
636
+ raise GitlabDeleteError ( "Object not yet created" )
626
637
627
638
return self .gitlab .delete (self )
628
639
@@ -783,8 +794,7 @@ def transfer_project(self, id):
783
794
url = '/groups/%d/projects/%d' % (self .id , id )
784
795
r = self .gitlab .rawPost (url , None )
785
796
if r .status_code != 201 :
786
- raise GitlabTransferProjectError ()
787
-
797
+ _raiseErrorFromResponse (r , GitlabTransferProjectError )
788
798
789
799
class Hook (GitlabObject ):
790
800
_url = '/hooks'
@@ -826,7 +836,7 @@ def protect(self, protect=True):
826
836
else :
827
837
del self .protected
828
838
else :
829
- raise GitlabProtectError
839
+ _raiseErrorFromResponse ( r , GitlabProtectError )
830
840
831
841
def unprotect (self ):
832
842
self .protect (False )
@@ -846,8 +856,9 @@ def diff(self):
846
856
r = self .gitlab .rawGet (url )
847
857
if r .status_code == 200 :
848
858
return r .json ()
859
+ else :
860
+ _raiseErrorFromResponse (r , GitlabGetError )
849
861
850
- raise GitlabGetError
851
862
852
863
def blob (self , filepath ):
853
864
url = '/projects/%(project_id)s/repository/blobs/%(commit_id)s' % \
@@ -856,8 +867,8 @@ def blob(self, filepath):
856
867
r = self .gitlab .rawGet (url )
857
868
if r .status_code == 200 :
858
869
return r .content
859
-
860
- raise GitlabGetError
870
+ else :
871
+ _raiseErrorFromResponse ( r , GitlabGetError )
861
872
862
873
863
874
class ProjectKey (GitlabObject ):
@@ -1022,7 +1033,7 @@ def Content(self):
1022
1033
if r .status_code == 200 :
1023
1034
return r .content
1024
1035
else :
1025
- raise GitlabGetError
1036
+ _raiseErrorFromResponse ( r , GitlabGetError )
1026
1037
1027
1038
def Note (self , id = None , ** kwargs ):
1028
1039
return ProjectSnippetNote ._getListOrObject (self .gitlab , id ,
@@ -1134,17 +1145,17 @@ def tree(self, path='', ref_name=''):
1134
1145
r = self .gitlab .rawGet (url )
1135
1146
if r .status_code == 200 :
1136
1147
return r .json ()
1137
-
1138
- raise GitlabGetError
1148
+ else :
1149
+ _raiseErrorFromResponse ( r , GitlabGetError )
1139
1150
1140
1151
def blob (self , sha , filepath ):
1141
1152
url = "%s/%s/repository/blobs/%s" % (self ._url , self .id , sha )
1142
1153
url += '?filepath=%s' % (filepath )
1143
1154
r = self .gitlab .rawGet (url )
1144
1155
if r .status_code == 200 :
1145
1156
return r .content
1146
-
1147
- raise GitlabGetError
1157
+ else :
1158
+ _raiseErrorFromResponse ( r , GitlabGetError )
1148
1159
1149
1160
def archive (self , sha = None ):
1150
1161
url = '/projects/%s/repository/archive' % self .id
@@ -1153,32 +1164,32 @@ def archive(self, sha=None):
1153
1164
r = self .gitlab .rawGet (url )
1154
1165
if r .status_code == 200 :
1155
1166
return r .content
1156
-
1157
- raise GitlabGetError
1167
+ else :
1168
+ _raiseErrorFromResponse ( r , GitlabGetError )
1158
1169
1159
1170
def create_file (self , path , branch , content , message ):
1160
1171
url = "/projects/%s/repository/files" % self .id
1161
1172
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
1162
1173
(path , branch , content , message )
1163
1174
r = self .gitlab .rawPost (url )
1164
1175
if r .status_code != 201 :
1165
- raise GitlabCreateError
1176
+ _raiseErrorFromResponse ( r , GitlabCreateError )
1166
1177
1167
1178
def update_file (self , path , branch , content , message ):
1168
1179
url = "/projects/%s/repository/files" % self .id
1169
1180
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
1170
1181
(path , branch , content , message )
1171
1182
r = self .gitlab .rawPut (url )
1172
1183
if r .status_code != 200 :
1173
- raise GitlabUpdateError
1184
+ _raiseErrorFromResponse ( r , GitlabUpdateError )
1174
1185
1175
1186
def delete_file (self , path , branch , message ):
1176
1187
url = "/projects/%s/repository/files" % self .id
1177
1188
url += "?file_path=%s&branch_name=%s&commit_message=%s" % \
1178
1189
(path , branch , message )
1179
1190
r = self .gitlab .rawDelete (url )
1180
1191
if r .status_code != 200 :
1181
- raise GitlabDeleteError
1192
+ _raiseErrorFromResponse ( r , GitlabDeleteError )
1182
1193
1183
1194
1184
1195
class TeamMember (GitlabObject ):
0 commit comments