Skip to content

Commit 6749859

Browse files
authored
Merge pull request python-gitlab#1059 from python-gitlab/fix/raise-from
chore: use raise..from for chained exceptions (python-gitlab#939)
2 parents 5979750 + 79fef26 commit 6749859

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

gitlab/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
606606
):
607607
try:
608608
return result.json()
609-
except Exception:
609+
except Exception as e:
610610
raise GitlabParsingError(
611611
error_message="Failed to parse the server message"
612-
)
612+
) from e
613613
else:
614614
return result
615615

@@ -686,8 +686,10 @@ def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs)
686686
try:
687687
if result.headers.get("Content-Type", None) == "application/json":
688688
return result.json()
689-
except Exception:
690-
raise GitlabParsingError(error_message="Failed to parse the server message")
689+
except Exception as e:
690+
raise GitlabParsingError(
691+
error_message="Failed to parse the server message"
692+
) from e
691693
return result
692694

693695
def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
@@ -722,8 +724,10 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
722724
)
723725
try:
724726
return result.json()
725-
except Exception:
726-
raise GitlabParsingError(error_message="Failed to parse the server message")
727+
except Exception as e:
728+
raise GitlabParsingError(
729+
error_message="Failed to parse the server message"
730+
) from e
727731

728732
def http_delete(self, path, **kwargs):
729733
"""Make a PUT request to the Gitlab server.
@@ -789,8 +793,10 @@ def _query(self, url, query_data=None, **kwargs):
789793

790794
try:
791795
self._data = result.json()
792-
except Exception:
793-
raise GitlabParsingError(error_message="Failed to parse the server message")
796+
except Exception as e:
797+
raise GitlabParsingError(
798+
error_message="Failed to parse the server message"
799+
) from e
794800

795801
self._current = 0
796802

gitlab/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ def __init__(self, gitlab_id=None, config_files=None):
6060
if self.gitlab_id is None:
6161
try:
6262
self.gitlab_id = self._config.get("global", "default")
63-
except Exception:
63+
except Exception as e:
6464
raise GitlabIDError(
6565
"Impossible to get the gitlab id (not specified in config file)"
66-
)
66+
) from e
6767

6868
try:
6969
self.url = self._config.get(self.gitlab_id, "url")
70-
except Exception:
70+
except Exception as e:
7171
raise GitlabDataError(
7272
"Impossible to get gitlab informations from "
7373
"configuration (%s)" % self.gitlab_id
74-
)
74+
) from e
7575

7676
self.ssl_verify = True
7777
try:

gitlab/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def wrapped_f(*args, **kwargs):
274274
try:
275275
return f(*args, **kwargs)
276276
except GitlabHttpError as e:
277-
raise error(e.error_message, e.response_code, e.response_body)
277+
raise error(e.error_message, e.response_code, e.response_body) from e
278278

279279
return wrapped_f
280280

gitlab/tests/test_exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
3+
from gitlab import exceptions
4+
5+
6+
class TestExceptions(unittest.TestCase):
7+
def test_error_raises_from_http_error(self):
8+
"""Methods decorated with @on_http_error should raise from GitlabHttpError."""
9+
10+
class TestError(Exception):
11+
pass
12+
13+
@exceptions.on_http_error(TestError)
14+
def raise_error_from_http_error():
15+
raise exceptions.GitlabHttpError
16+
17+
with self.assertRaises(TestError) as context:
18+
raise_error_from_http_error()
19+
self.assertIsInstance(context.exception.__cause__, exceptions.GitlabHttpError)

gitlab/v4/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,14 +2843,14 @@ def set_release_description(self, description, **kwargs):
28432843
path, post_data=data, **kwargs
28442844
)
28452845
except exc.GitlabHttpError as e:
2846-
raise exc.GitlabCreateError(e.response_code, e.error_message)
2846+
raise exc.GitlabCreateError(e.response_code, e.error_message) from e
28472847
else:
28482848
try:
28492849
server_data = self.manager.gitlab.http_put(
28502850
path, post_data=data, **kwargs
28512851
)
28522852
except exc.GitlabHttpError as e:
2853-
raise exc.GitlabUpdateError(e.response_code, e.error_message)
2853+
raise exc.GitlabUpdateError(e.response_code, e.error_message) from e
28542854
self.release = server_data
28552855

28562856

0 commit comments

Comments
 (0)