summaryrefslogtreecommitdiff
path: root/server/error.py
blob: 04e9968b698c10e49c16d02ca318d02ceebb5e03 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'''
    Decorators that are used to manage errors
    during runtime. This includes logging the
    errors & providing information about the
    severity & the next step.

    Verified: 2021 February 7
    * Follows PEP8
    * Tested Platforms
        * Windows 10
'''

from server.controller import Controller
from server.state import ServerStatic
from server.console import Console
from shared.helper import Helper
from shared.state import Static

import traceback
import inspect
import sys


class ServerError:

    @staticmethod
    def general(callback):
        def wrapper(request=None, gui_call=None, custom=None):
            try:
                params = len(inspect.signature(callback).parameters)

                if params == 0:
                    return callback()
                if params == 1:
                    return callback(request)
                if params == 2:
                    return callback(request, gui_call)
                else:
                    return callback(request, gui_call, custom)
            except Exception as error:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)

                return Console.printf(Helper.join(
                    'Server Side Execution Failed',
                    f'{type(error).__name__}: {error}'
                ), Static.DANGER, ret=gui_call)

        return wrapper

    @staticmethod
    def quiet(callback):
        def wrapper(*args, **kwargs):
            try:
                return callback(*args, **kwargs)
            except Exception:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)

        return wrapper

    @staticmethod
    def quiet_thread(callback):
        def wrapper(*args, **kwargs):
            try:
                return callback(*args, **kwargs)
            except Exception:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)
                sys.exit()

        return wrapper

    @staticmethod
    def critical(callback):
        def wrapper(*args, **kwargs):
            try:
                return callback(*args, **kwargs)
            except KeyboardInterrupt:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)

                Console.printf(Helper.join(
                    'Keyboard Interrupt Noticed',
                    f'Exiting {ServerStatic.NAME}'
                ), Static.DANGER, False, newline=True)

                Controller.exit_program()
            except Exception as error:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)

                Console.printf(Helper.join(
                    '[STDIN] Internal Server Error',
                    f'{type(error).__name__}: {error}',
                    f'Exiting {ServerStatic.NAME}'
                ), Static.DANGER, False, newline=True)

                Controller.exit_program()

        return wrapper

    @staticmethod
    def thread(callback):
        def wrapper(*args, **kwargs):
            try:
                return callback(*args, **kwargs)
            except Exception as error:
                Console.log(traceback.format_exc(),
                            log_type=ServerStatic.TRACEBACK)

                Console.printf(Helper.join(
                    '[THREAD] Internal Server Error',
                    f'{type(error).__name__}: {error}',
                    f'{ServerStatic.NAME} Restart Recommended'
                ), Static.DANGER, newline=True)

        return wrapper