Source code for diplomacy.utils.exceptions

# ==============================================================================
# Copyright (C) 2019 - Philip Paquette, Steven Bocco
#
#  This program is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
#  details.
#
#  You should have received a copy of the GNU Affero General Public License along
#  with this program.  If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Exceptions used in diplomacy network code. """

[docs]class DiplomacyException(Exception): """ Diplomacy network code exception. """ def __init__(self, message=''): self.message = (message or self.__doc__).strip() super(DiplomacyException, self).__init__(self.message)
[docs]class AlreadyScheduledException(DiplomacyException): """ Cannot add a data already scheduled. """
[docs]class CommonKeyException(DiplomacyException): """Common key error.""" def __init__(self, key): super(CommonKeyException, self).__init__('Forbidden common key in two dicts (%s)' % key)
[docs]class KeyException(DiplomacyException): """ Key error. """ def __init__(self, key): super(KeyException, self).__init__('Key error: %s' % key)
[docs]class LengthException(DiplomacyException): """ Length error. """ def __init__(self, expected_length, given_length): super(LengthException, self).__init__('Expected length %d, got %d.' % (expected_length, given_length))
[docs]class NaturalIntegerException(DiplomacyException): """ Expected a positive integer (int >= 0). """ def __init__(self, integer_name=''): super(NaturalIntegerException, self).__init__( ('Integer error: %s.%s' % (integer_name, self.__doc__)) if integer_name else '')
[docs]class NaturalIntegerNotNullException(NaturalIntegerException): """ Expected a strictly positive integer (int > 0). """
[docs]class RandomPowerException(DiplomacyException): """ No enough playable powers to select random powers. """ def __init__(self, nb_powers, nb_available_powers): super(RandomPowerException, self).__init__('Cannot randomly select %s power(s) in %s available power(s).' % (nb_powers, nb_available_powers))
[docs]class TypeException(DiplomacyException): """ Type error. """ def __init__(self, expected_type, given_type): super(TypeException, self).__init__('Expected type %s, got type %s' % (expected_type, given_type))
[docs]class ValueException(DiplomacyException): """ Value error. """ def __init__(self, expected_values, given_value): super(ValueException, self).__init__('Forbidden value %s, expected: %s' % (given_value, ', '.join(str(v) for v in expected_values)))
[docs]class NotificationException(DiplomacyException): """ Unknown notification. """
[docs]class ResponseException(DiplomacyException): """ Unknown response. """
[docs]class RequestException(ResponseException): """ Unknown request. """
[docs]class AdminTokenException(ResponseException): """ Invalid token for admin operations. """
[docs]class DaidePortException(ResponseException): """ Daide server not started for the game """
[docs]class GameCanceledException(ResponseException): """ Game was cancelled. """
[docs]class GameCreationException(ResponseException): """ Cannot create more games on that server. """
[docs]class GameFinishedException(ResponseException): """ This game is finished. """
[docs]class GameIdException(ResponseException): """ Invalid game ID. """
[docs]class GameJoinRoleException(ResponseException): """ A token can have only one role inside a game: player, observer or omniscient. """
[docs]class GameRoleException(ResponseException): """ Game role does not accepts this action. """
[docs]class GameMasterTokenException(ResponseException): """ Invalid token for master operations. """
[docs]class GameNotPlayingException(ResponseException): """ Game not playing. """
[docs]class GameObserverException(ResponseException): """ Disallowed observation for non-master users. """
[docs]class GamePhaseException(ResponseException): """ Data does not match current game phase. """ def __init__(self, expected=None, given=None): message = self.__doc__.strip() # This is to prevent an unexpected Pycharm warning about message type. if isinstance(message, bytes): message = message.decode() if expected is not None: message += ' Expected: %s' % expected if given is not None: message += ' Given: %s' % given super(GamePhaseException, self).__init__(message)
[docs]class GamePlayerException(ResponseException): """ Invalid player. """
[docs]class GameRegistrationPasswordException(ResponseException): """ Invalid game registration password. """
[docs]class GameSolitaireException(ResponseException): """ A solitaire game does not accepts players. """
[docs]class GameTokenException(ResponseException): """ Invalid token for this game. """
[docs]class MapIdException(ResponseException): """ Invalid map ID. """
[docs]class MapPowerException(ResponseException): """ Invalid map power. """ def __init__(self, power_name): super(MapPowerException, self).__init__('Invalid map power %s' % power_name)
[docs]class FolderException(ResponseException): """ Given folder not available in server. """ def __init__(self, folder_path): super(FolderException, self).__init__('Given folder not available in server: %s' % folder_path)
[docs]class ServerRegistrationException(ResponseException): """ Registration currently not allowed on this server. """
[docs]class TokenException(ResponseException): """ Invalid token. """
[docs]class UserException(ResponseException): """ Invalid user. """
[docs]class PasswordException(ResponseException): """ Password must not be empty. """
[docs]class ServerDirException(ResponseException): """ Error with working folder. """ def __init__(self, server_dir): super(ServerDirException, self).__init__("No server directory available at path %s" % server_dir)