From 7577c91c5819c8f37acbf4d4c3eee3ffd84522a0 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Sat, 25 Jun 2022 10:02:53 -0700 Subject: [PATCH] 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. --- gitlab/exceptions.py | 8 ++++++++ tests/unit/test_cli.py | 5 ++--- tests/unit/test_exceptions.py | 7 +++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 8465838e2..b39dfc20b 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -18,6 +18,10 @@ import functools from typing import Any, Callable, cast, Optional, Type, TYPE_CHECKING, TypeVar, Union +from . import _version as _gl_version + +_PG_VERSION = f" (python-gitlab version: {_gl_version.__version__})" + class GitlabError(Exception): def __init__( @@ -27,6 +31,10 @@ def __init__( response_body: Optional[bytes] = None, ) -> None: + if isinstance(error_message, str): + error_message += _PG_VERSION + else: + error_message += bytes(_PG_VERSION, encoding="ascii") Exception.__init__(self, error_message) # Http status code self.response_code = response_code diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index ef33b5db9..769b087d0 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -24,8 +24,7 @@ import pytest -from gitlab import cli -from gitlab.exceptions import GitlabError +from gitlab import cli, exceptions @pytest.mark.parametrize( @@ -71,7 +70,7 @@ def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource): "message,error,expected", [ ("foobar", None, "foobar\n"), - ("foo", GitlabError("bar"), "foo (bar)\n"), + ("foo", exceptions.GitlabError("bar"), f"foo (bar{exceptions._PG_VERSION})\n"), ], ) def test_die(message, error, expected): diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 6ef093950..dd1a74819 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -6,8 +6,11 @@ @pytest.mark.parametrize( "kwargs,expected", [ - ({"error_message": "foo"}, "foo"), - ({"error_message": "foo", "response_code": "400"}, "400: foo"), + ({"error_message": "foo"}, f"foo{exceptions._PG_VERSION}"), + ( + {"error_message": "foo", "response_code": "400"}, + f"400: foo{exceptions._PG_VERSION}", + ), ], ) def test_gitlab_error(kwargs, expected):