From c4c5c831c6549a12004baf33638fd469a1e8a886 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 1 Jul 2022 10:31:23 +0400 Subject: [PATCH] Errors, bedrock whitelist bug --- python_aternos/__init__.py | 9 ++---- python_aternos/atconnect.py | 2 ++ python_aternos/aterrors.py | 60 ++++++++++++++++++++++++++----------- python_aternos/atplayers.py | 2 +- python_aternos/atserver.py | 52 ++++---------------------------- 5 files changed, 55 insertions(+), 70 deletions(-) diff --git a/python_aternos/__init__.py b/python_aternos/__init__.py index e1cb3f7..70196cb 100644 --- a/python_aternos/__init__.py +++ b/python_aternos/__init__.py @@ -21,10 +21,7 @@ from .aterrors import CloudflareError from .aterrors import CredentialsError from .aterrors import TokenError from .aterrors import ServerError -from .aterrors import ServerEulaError -from .aterrors import ServerRunningError -from .aterrors import ServerSoftwareError -from .aterrors import ServerStorageError +from .aterrors import ServerStartError from .aterrors import FileError from .aterrors import PermissionError from .atjsparse import exec, atob @@ -41,8 +38,8 @@ __all__ = [ 'PlayersList', 'AternosConfig', 'AternosWss', 'FileManager', 'AternosFile', 'AternosError', 'CloudflareError', 'CredentialsError', 'TokenError', - 'ServerError', 'ServerEulaError', 'ServerRunningError', - 'ServerSoftwareError', 'ServerStorageError', 'FileError', + 'ServerError', 'ServerStartError', 'FileError', + 'PermissionError', 'exec', 'atob', 'to_ecma5_function', 'Edition', 'Status', 'Lists', diff --git a/python_aternos/atconnect.py b/python_aternos/atconnect.py index 0677d22..61bd4e3 100644 --- a/python_aternos/atconnect.py +++ b/python_aternos/atconnect.py @@ -249,4 +249,6 @@ class AternosConnect: f'{method} completed with {req.status_code} status' ) + req.raise_for_status() + return req diff --git a/python_aternos/aterrors.py b/python_aternos/aterrors.py index 7c39495..7dc6a93 100644 --- a/python_aternos/aterrors.py +++ b/python_aternos/aterrors.py @@ -1,3 +1,6 @@ +from typing import Final + + class AternosError(Exception): """Common error class""" @@ -23,31 +26,53 @@ class TokenError(AternosError): class ServerError(AternosError): - """Common class for server errors""" + """Common class for server errors + + :param reason: Code which contains error reason + :type reason: str + :param message: Error message, defaults to '' + :type message: str, optional + """ + + def __init__(self, reason: str, message: str = '') -> None: + + self.reason = reason + super().__init__(message) -class ServerEulaError(ServerError): +class ServerStartError(AternosError): - """Raised when trying to start without - confirming Mojang EULA""" + """Raised when Aternos can not start Minecraft server + :param reason: Code which contains error reason + :type reason: str + """ -class ServerRunningError(ServerError): + MESSAGE: Final = 'Unable to start server, code: {}' + reason_msg = { - """Raised when trying to start - already running server""" + 'eula': + 'EULA was not accepted. ' + 'Use start(accepteula=True)', + 'already': 'Server is already running', + 'wrongversion': 'Incorrect software version installed', -class ServerSoftwareError(ServerError): + 'file': + 'File server is unavailbale, ' + 'view https://status.aternos.gmbh', - """Raised when Aternos notifies about - incorrect software version""" + 'size': 'Available storage size limit (4 GB) was reached' + } + def __init__(self, reason: str) -> None: -class ServerStorageError(ServerError): - - """Raised when Aternos notifies about - violation of storage limits (4 GB for now)""" + super().__init__( + reason, + self.reason_msg.get( + reason, self.MESSAGE.format(reason) + ) + ) class FileError(AternosError): @@ -55,7 +80,8 @@ class FileError(AternosError): """Raised when trying to execute a disallowed by Aternos file operation""" - + class PermissionError(AternosError): - """Raised when trying to execute a non-allowed - command on a server (Think friends permissions)""" + + """Raised when trying to execute a disallowed command, + usually because of shared access rights""" diff --git a/python_aternos/atplayers.py b/python_aternos/atplayers.py index 7616e74..e174224 100644 --- a/python_aternos/atplayers.py +++ b/python_aternos/atplayers.py @@ -35,7 +35,7 @@ class PlayersList: self.atserv = atserv self.lst = Lists(lst) - + common_whl = (self.lst == Lists.whl) bedrock = (atserv.edition == Edition.bedrock) if common_whl and bedrock: diff --git a/python_aternos/atserver.py b/python_aternos/atserver.py index df7f6e4..7175fbe 100644 --- a/python_aternos/atserver.py +++ b/python_aternos/atserver.py @@ -4,11 +4,7 @@ from requests import Response from typing import Optional, List from .atconnect import AternosConnect -from .aterrors import ServerError -from .aterrors import ServerEulaError -from .aterrors import ServerRunningError -from .aterrors import ServerSoftwareError -from .aterrors import ServerStorageError +from .aterrors import ServerStartError from .atfm import FileManager from .atconf import AternosConfig from .atplayers import PlayersList @@ -99,16 +95,8 @@ class AternosServer: :param accepteula: Automatically accept the Mojang EULA, defaults to `True` :type accepteula: bool, optional - :raises ServerEulaError: When trying to start a server - without accepting the Mojang EULA - :raises ServerRunningError: When trying to start a server - which is alreday running - :raises ServerSoftwareError: When Aternos notifies about - incorrect software version - :raises ServerStorageError: When Aternos notifies about - voilation of storage limits (4 GB for now) - :raises ServerError: When API is unable to start a Minecraft server - due to unavailability of Aternos' file servers or other problems + :raises ServerStartError: When Aternos + is unable to start the server """ startreq = self.atserver_request( @@ -120,42 +108,14 @@ class AternosServer: if startresult['success']: return + error = startresult['error'] if error == 'eula' and accepteula: self.eula() - self.start(accepteula=False) + return self.start(accepteula=False) - elif error == 'eula': - raise ServerEulaError( - 'EULA was not accepted. Use start(accepteula=True)' - ) - - elif error == 'already': - raise ServerRunningError( - 'Server is already running' - ) - - elif error == 'wrongversion': - raise ServerSoftwareError( - 'Incorrect software version installed' - ) - - elif error == 'file': - raise ServerError( - 'File server is unavailbale, view https://status.aternos.gmbh' - ) - - elif error == 'size': - raise ServerStorageError( - f'Available storage size is 4GB, ' - f'your server used: {startresult["size"]}' - ) - - else: - raise ServerError( - f'Unable to start server, code: {error}' - ) + raise ServerStartError(error) def confirm(self) -> None: