Skip to content

Commit 7577c91

Browse files
chore: add the version of python-gitlab to GitlabError
People will post tracebacks of exceptions but not post information on the version of python-gitlab used. Add the version of python-gitlab to the GitlabError exception message.
1 parent f9b7c7b commit 7577c91

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

gitlab/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import functools
1919
from typing import Any, Callable, cast, Optional, Type, TYPE_CHECKING, TypeVar, Union
2020

21+
from . import _version as _gl_version
22+
23+
_PG_VERSION = f" (python-gitlab version: {_gl_version.__version__})"
24+
2125

2226
class GitlabError(Exception):
2327
def __init__(
@@ -27,6 +31,10 @@ def __init__(
2731
response_body: Optional[bytes] = None,
2832
) -> None:
2933

34+
if isinstance(error_message, str):
35+
error_message += _PG_VERSION
36+
else:
37+
error_message += bytes(_PG_VERSION, encoding="ascii")
3038
Exception.__init__(self, error_message)
3139
# Http status code
3240
self.response_code = response_code

tests/unit/test_cli.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
import pytest
2626

27-
from gitlab import cli
28-
from gitlab.exceptions import GitlabError
27+
from gitlab import cli, exceptions
2928

3029

3130
@pytest.mark.parametrize(
@@ -71,7 +70,7 @@ def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource):
7170
"message,error,expected",
7271
[
7372
("foobar", None, "foobar\n"),
74-
("foo", GitlabError("bar"), "foo (bar)\n"),
73+
("foo", exceptions.GitlabError("bar"), f"foo (bar{exceptions._PG_VERSION})\n"),
7574
],
7675
)
7776
def test_die(message, error, expected):

tests/unit/test_exceptions.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
@pytest.mark.parametrize(
77
"kwargs,expected",
88
[
9-
({"error_message": "foo"}, "foo"),
10-
({"error_message": "foo", "response_code": "400"}, "400: foo"),
9+
({"error_message": "foo"}, f"foo{exceptions._PG_VERSION}"),
10+
(
11+
{"error_message": "foo", "response_code": "400"},
12+
f"400: foo{exceptions._PG_VERSION}",
13+
),
1114
],
1215
)
1316
def test_gitlab_error(kwargs, expected):

0 commit comments

Comments
 (0)