Skip to content

Commit 79fef26

Browse files
committed
chore: use raise..from for chained exceptions (#939)
1 parent c5904c4 commit 79fef26

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
@@ -605,10 +605,10 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
605605
):
606606
try:
607607
return result.json()
608-
except Exception:
608+
except Exception as e:
609609
raise GitlabParsingError(
610610
error_message="Failed to parse the server message"
611-
)
611+
) from e
612612
else:
613613
return result
614614

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

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

727731
def http_delete(self, path, **kwargs):
728732
"""Make a PUT request to the Gitlab server.
@@ -788,8 +792,10 @@ def _query(self, url, query_data=None, **kwargs):
788792

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

794800
self._current = 0
795801

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
@@ -270,7 +270,7 @@ def wrapped_f(*args, **kwargs):
270270
try:
271271
return f(*args, **kwargs)
272272
except GitlabHttpError as e:
273-
raise error(e.error_message, e.response_code, e.response_body)
273+
raise error(e.error_message, e.response_code, e.response_body) from e
274274

275275
return wrapped_f
276276

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
@@ -2720,14 +2720,14 @@ def set_release_description(self, description, **kwargs):
27202720
path, post_data=data, **kwargs
27212721
)
27222722
except exc.GitlabHttpError as e:
2723-
raise exc.GitlabCreateError(e.response_code, e.error_message)
2723+
raise exc.GitlabCreateError(e.response_code, e.error_message) from e
27242724
else:
27252725
try:
27262726
server_data = self.manager.gitlab.http_put(
27272727
path, post_data=data, **kwargs
27282728
)
27292729
except exc.GitlabHttpError as e:
2730-
raise exc.GitlabUpdateError(e.response_code, e.error_message)
2730+
raise exc.GitlabUpdateError(e.response_code, e.error_message) from e
27312731
self.release = server_data
27322732

27332733

0 commit comments

Comments
 (0)