diff options
author | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
---|---|---|
committer | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
commit | 58ebd3bc0f00c532e97e9a5571471ffab87934ba (patch) | |
tree | 6e099e59af07206df6edf2b0c585d0c5a466d4bd /client/error.py |
Diffstat (limited to 'client/error.py')
-rw-r--r-- | client/error.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/client/error.py b/client/error.py new file mode 100644 index 0000000..2956271 --- /dev/null +++ b/client/error.py @@ -0,0 +1,72 @@ +''' + Error handling client decorators, to provide + useful information sent back to the server or + handle critical errors accordingly. + + Verified: 2020 December 30 & 2021 February 6 + * Follows PEP8 + * Tested Platforms + * Windows 10 +''' + +from client.state import ClientStatic +from shared.helper import Helper +from shared.state import Static +from shared.data import Data + +import sys + +if Static.EXE: + from client.helper import ClientHelper + + import time + + +class ReconnectError(Exception): + pass + + +class ClientError: + + if Static.EXE: + __RECONNECT_TIMER = 10 + + @staticmethod + def general(callback): + def wrapper(*args, **kwargs): + try: + return callback(*args, **kwargs) + except Exception as error: + return Data.parse(Helper.join( + 'Client Side Execution Failed', + f'{type(error).__name__}: {error}' + ), status=Static.DANGER) + + return wrapper + + @staticmethod + def critical(callback): + def wrapper(*args, **kwargs): + try: + return callback(*args, **kwargs) + except SystemExit: + raise + except ReconnectError: + if Static.EXE: + ClientHelper.restart() + else: + sys.exit() + except TimeoutError: + if Static.EXE: + time.sleep(ClientError.__RECONNECT_TIMER) + ClientHelper.restart() + else: + sys.exit() + except Exception: + if Static.EXE and ClientStatic.STICKY: + time.sleep(ClientError.__RECONNECT_TIMER) + ClientHelper.restart() + else: + sys.exit() + + return wrapper |