/ github / GithubException.py
GithubException.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net>                 #
  4  # Copyright 2012 Zearin <zearin@gonk.net>                                      #
  5  # Copyright 2013 AKFish <akfish@gmail.com>                                     #
  6  # Copyright 2013 Cameron White <cawhite@pdx.edu>                               #
  7  # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net>                 #
  8  # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
  9  # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
 10  # Copyright 2016 humbug <bah>                                                  #
 11  # Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
 12  #                                                                              #
 13  # This file is part of PyGithub.                                               #
 14  # http://pygithub.readthedocs.io/                                              #
 15  #                                                                              #
 16  # PyGithub is free software: you can redistribute it and/or modify it under    #
 17  # the terms of the GNU Lesser General Public License as published by the Free  #
 18  # Software Foundation, either version 3 of the License, or (at your option)    #
 19  # any later version.                                                           #
 20  #                                                                              #
 21  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 22  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 23  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 24  # details.                                                                     #
 25  #                                                                              #
 26  # You should have received a copy of the GNU Lesser General Public License     #
 27  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 28  #                                                                              #
 29  ################################################################################
 30  import json
 31  from typing import Any, Dict, List, Optional, Tuple, Type, Union
 32  
 33  
 34  class GithubException(Exception):
 35      """
 36      Error handling in PyGithub is done with exceptions. This class is the base of all exceptions raised by PyGithub (but :class:`github.GithubException.BadAttributeException`).
 37  
 38      Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.
 39      """
 40  
 41      def __init__(
 42          self,
 43          status: int,
 44          data: Any = None,
 45          headers: Optional[Dict[str, str]] = None,
 46          message: Optional[str] = None,
 47      ):
 48          super().__init__()
 49          self.__status = status
 50          self.__data = data
 51          self.__headers = headers
 52          self.__message = message
 53          self.args = (status, data, headers, message)
 54  
 55      @property
 56      def message(self) -> Optional[str]:
 57          return self.__message
 58  
 59      @property
 60      def status(self) -> int:
 61          """
 62          The status returned by the Github API
 63          """
 64          return self.__status
 65  
 66      @property
 67      def data(self) -> Any:
 68          """
 69          The (decoded) data returned by the Github API
 70          """
 71          return self.__data
 72  
 73      @property
 74      def headers(self) -> Optional[Dict[str, str]]:
 75          """
 76          The headers returned by the Github API
 77          """
 78          return self.__headers
 79  
 80      def __repr__(self) -> str:
 81          return f"{self.__class__.__name__}({self.__str__()})"
 82  
 83      def __str__(self) -> str:
 84          if self.__message:
 85              msg = f"{self.__message}: {self.status}"
 86          else:
 87              msg = f"{self.status}"
 88  
 89          if self.data is not None:
 90              msg += " " + json.dumps(self.data)
 91  
 92          return msg
 93  
 94  
 95  class BadCredentialsException(GithubException):
 96      """
 97      Exception raised in case of bad credentials (when Github API replies with a 401 or 403 HTML status)
 98      """
 99  
100  
101  class UnknownObjectException(GithubException):
102      """
103      Exception raised when a non-existing object is requested (when Github API replies with a 404 HTML status)
104      """
105  
106  
107  class BadUserAgentException(GithubException):
108      """
109      Exception raised when request is sent with a bad user agent header (when Github API replies with a 403 bad user agent HTML status)
110      """
111  
112  
113  class RateLimitExceededException(GithubException):
114      """
115      Exception raised when the rate limit is exceeded (when Github API replies with a 403 rate limit exceeded HTML status)
116      """
117  
118  
119  class BadAttributeException(Exception):
120      """
121      Exception raised when Github returns an attribute with the wrong type.
122      """
123  
124      def __init__(
125          self,
126          actualValue: Any,
127          expectedType: Union[
128              Dict[Tuple[Type[str], Type[str]], Type[dict]],
129              Tuple[Type[str], Type[str]],
130              List[Type[dict]],
131              List[Tuple[Type[str], Type[str]]],
132          ],
133          transformationException: Optional[Exception],
134      ):
135          self.__actualValue = actualValue
136          self.__expectedType = expectedType
137          self.__transformationException = transformationException
138  
139      @property
140      def actual_value(self) -> Any:
141          """
142          The value returned by Github
143          """
144          return self.__actualValue
145  
146      @property
147      def expected_type(
148          self,
149      ) -> Union[
150          List[Type[dict]],
151          Tuple[Type[str], Type[str]],
152          Dict[Tuple[Type[str], Type[str]], Type[dict]],
153          List[Tuple[Type[str], Type[str]]],
154      ]:
155          """
156          The type PyGithub expected
157          """
158          return self.__expectedType
159  
160      @property
161      def transformation_exception(self) -> Optional[Exception]:
162          """
163          The exception raised when PyGithub tried to parse the value
164          """
165          return self.__transformationException
166  
167  
168  class TwoFactorException(GithubException):
169      """
170      Exception raised when Github requires a onetime password for two-factor authentication
171      """
172  
173  
174  class IncompletableObject(GithubException):
175      """
176      Exception raised when we can not request an object from Github because the data returned did not include a URL
177      """