summaryrefslogtreecommitdiff
path: root/client/error.py
blob: 2956271d1a658bb36fa847b5c603711567756685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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