10000 chore: use raise..from for chained exceptions (#939) by nejch · Pull Request #1059 · python-gitlab/python-gitlab · GitHub
[go: up one dir, main page]

Skip to content

chore: use raise..from for chained exceptions (#939) #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,10 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
):
try:
return result.json()
except Exception:
except Exception as e:
raise GitlabParsingError(
error_message="Failed to parse the server message"
)
) from e
else:
return result

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

def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
Expand Down Expand Up @@ -721,8 +723,10 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
)
try:
return result.json()
except Exception:
raise GitlabParsingError(error_message="Failed to parse the server message")
except Exception as e:
raise GitlabParsingError(
error_message="Failed to parse the server message"
) from e

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

try:
self._data = result.json()
except Exception:
raise GitlabParsingError(error_message="Failed to parse the server message")
except Exception as e:
raise GitlabParsingError(
error_message="Failed to parse the server message"
) from e

self._current = 0

Expand Down
8 changes: 4 additions & 4 deletions gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ def __init__(self, gitlab_id=None, config_files=None):
if self.gitlab_id is None:
try:
self.gitlab_id = self._config.get("global", "default")
except Exception:
except Exception as e:
raise GitlabIDError(
"Impossible to get the gitlab id (not specified in config file)"
)
) from e

try:
self.url = self._config.get(self.gitlab_id, "url")
except Exception:
except Exception as e:
raise GitlabDataError(
"Impossible to get gitlab informations from "
"configuration (%s)" % self.gitlab_id
)
) from e

self.ssl_verify = True
try:
Expand Down
2 changes: 1 addition & 1 deletion gitlab/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def wrapped_f(*args, **kwargs):
try:
return f(*args, **kwargs)
except GitlabHttpError as e:
raise error(e.error_message, e.response_code, e.response_body)
raise error(e.error_message, e.response_code, e.response_body) from e

return wrapped_f

Expand Down
19 changes: 19 additions & 0 deletions gitlab/tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from gitlab import exceptions


class TestExceptions(unittest.TestCase):
def test_error_raises_from_http_error(self):
"""Methods decorated with @on_http_error should raise from GitlabHttpError."""

class TestError(Exception):
pass

@exceptions.on_http_error(TestError)
def raise_error_from_http_error():
raise exceptions.GitlabHttpError

with self.assertRaises(TestError) as context:
raise_error_from_http_error()
self.assertIsInstance(context.exception.__cause__, exceptions.GitlabHttpError)
4 changes: 2 additions & 2 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2720,14 +2720,14 @@ def set_release_description(self, description, **kwargs):
path, post_data=data, **kwargs
)
except exc.GitlabHttpError as e:
raise exc.GitlabCreateError(e.response_code, e.error_message)
raise exc.GitlabCreateError(e.response_code, e.error_message) from e
else:
try:
server_data = self.manager.gitlab.http_put(
path, post_data=data, **kwargs
)
except exc.GitlabHttpError as e:
raise exc.GitlabUpdateError(e.response_code, e.error_message)
raise exc.GitlabUpdateError(e.response_code, e.error_message) from e
self.release = server_data


Expand Down
0