diff --git a/github/GithubException.py b/github/GithubException.py index 717102ca..42f50d2c 100644 --- a/github/GithubException.py +++ b/github/GithubException.py @@ -27,7 +27,6 @@ # along with PyGithub. If not, see . # # # ################################################################################ - import json from typing import Any, Dict, List, Optional, Tuple, Type, Union @@ -42,14 +41,20 @@ class GithubException(Exception): def __init__( self, status: int, - data: Union[str, Dict[str, Union[str, List[str], List[Dict[str, str]]]]], - headers: Optional[Dict[str, str]], + data: Any = None, + headers: Optional[Dict[str, str]] = None, + message: Optional[str] = None, ): super().__init__() self.__status = status self.__data = data self.__headers = headers - self.args = (status, data, headers) + self.__message = message + self.args = (status, data, headers, message) + + @property + def message(self) -> Optional[str]: + return self.__message @property def status(self) -> int: @@ -59,9 +64,7 @@ class GithubException(Exception): return self.__status @property - def data( - self, - ) -> Union[str, Dict[str, Union[str, List[str], List[Dict[str, str]]]]]: + def data(self) -> Any: """ The (decoded) data returned by the Github API """ @@ -74,8 +77,19 @@ class GithubException(Exception): """ return self.__headers + def __repr__(self) -> str: + return f"{self.__class__.__name__}({self.__str__()})" + def __str__(self) -> str: - return f"{self.status} {json.dumps(self.data)}" + if self.__message: + msg = f"{self.__message}: {self.status}" + else: + msg = f"{self.status}" + + if self.data is not None: + msg += " " + json.dumps(self.data) + + return msg class BadCredentialsException(GithubException): diff --git a/github/GithubObject.py b/github/GithubObject.py index 304ceb33..2717cde2 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -403,7 +403,7 @@ class CompletableGithubObject(GithubObject): def __complete(self): if self._url.value is None: - raise IncompletableObject(400, "Returned object contains no URL", None) + raise IncompletableObject(400, message="Returned object contains no URL") headers, data = self._requester.requestJsonAndCheck("GET", self._url.value) self._storeAndUseAttributes(headers, data) self.__completed = True diff --git a/github/Requester.py b/github/Requester.py index 429cf075..532f5c5d 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -538,7 +538,7 @@ class Requester: status: int, headers: Dict[str, Any], output: Dict[str, Any], - ) -> Any: + ) -> GithubException.GithubException: message = output.get("message", "").lower() if output is not None else "" exc = GithubException.GithubException diff --git a/tests/Requester.py b/tests/Requester.py index 926a61ea..5c1b0576 100644 --- a/tests/Requester.py +++ b/tests/Requester.py @@ -344,7 +344,7 @@ class Requester(Framework.TestCase): with self.subTest(status=status): exc = self.g._Github__requester.createException(status, {}, None) self.assertException( - exc, github.GithubException, status, None, {}, f"{status} null" + exc, github.GithubException, status, None, {}, f"{status}" )