summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/common/trex_exceptions.py
blob: 0de3841159183190f269892a6f7cb86c1c65bd8f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/router/bin/python

#from rpc_exceptions import RPCExceptionHandler, WrappedRPCError

from jsonrpclib import Fault, ProtocolError, AppError

class RPCError(Exception):
    """
    This is the general RPC error exception class from which :exc:`trex_exceptions.TRexException` inherits. 

    Every exception in this class has as error format according to JSON-RPC convention convention: code, message and data.

    """
    def __init__(self, code, message, remote_data = None):
        self.code   = code
        self.msg    = message or self._default_message
        self.data   = remote_data
        self.args   = (code, self.msg, remote_data)

    def __str__(self):
        return self.__repr__()
    def __repr__(self):
        if self.args[2] is not None:
            return u"[errcode:%r] %r. Extended data: %r" % (self.args[0], self.args[1], self.args[2])
        else:
            return u"[errcode:%r] %r" % (self.args[0], self.args[1])

class TRexException(RPCError):
    """ 
    This is the most general TRex exception.
    
    All exceptions inherits from this class has an error code and a default message which describes the most common use case of the error.

    This exception isn't used by default and will only when an unrelated to ProtocolError will occur, and it can't be resolved to any of the deriviate exceptions.

    """
    code = -10
    _default_message = 'TRex encountered an unexpected error. please contact TRex dev team.'
    # api_name = 'TRex'

class TRexError(TRexException):
    """ 
    This is the most general TRex exception.

    This exception isn't used by default and will only when an unrelated to ProtocolError will occur, and it can't be resolved to any of the deriviate exceptions.
    """
    code = -11
    _default_message = 'TRex run failed due to wrong input parameters, or due to reachability issues.'

class TRexWarning(TRexException):
    """ Indicates a warning from TRex server. When this exception raises it normally used to indicate required data isn't ready yet """
    code = -12
    _default_message = 'TRex is starting (data is not available yet).'

class TRexRequestDenied(TRexException):
    """ Indicates the desired reques was denied by the server """
    code = -33
    _default_message = 'TRex desired request denied because the requested resource is already taken. Try again once TRex is back in IDLE state.'

class TRexInUseError(TRexException):
    """
    Indicates that TRex is currently in use

    """
    code = -13
    _default_message = 'TRex is already being used by another user or process. Try again once TRex is back in IDLE state.'

class TRexRunFailedError(TRexException):
    """ Indicates that TRex has failed due to some reason. This Exception is used when TRex process itself terminates due to unknown reason """
    code = -14
    _default_message = ''

class TRexIncompleteRunError(TRexException):
    """ 
    Indicates that TRex has failed due to some reason.
    This Exception is used when TRex process itself terminated with error fault or it has been terminated by an external intervention in the OS.

    """
    code = -15
    _default_message = 'TRex run was terminated unexpectedly by outer process or by the hosting OS'

EXCEPTIONS = [TRexException, TRexError, TRexWarning, TRexInUseError, TRexRequestDenied, TRexRunFailedError, TRexIncompleteRunError]

class CExceptionHandler(object):
    """ 
    CExceptionHandler is responsible for generating TRex API related exceptions in client side.
    """
    def __init__(self, exceptions):
        """ 
        Instatiate a CExceptionHandler object

        :parameters:

         exceptions : list
            a list of all TRex acceptable exception objects.
            
            default list:
               - :exc:`trex_exceptions.TRexException`
               - :exc:`trex_exceptions.TRexError`
               - :exc:`trex_exceptions.TRexWarning`
               - :exc:`trex_exceptions.TRexInUseError`
               - :exc:`trex_exceptions.TRexRequestDenied`
               - :exc:`trex_exceptions.TRexRunFailedError`
               - :exc:`trex_exceptions.TRexIncompleteRunError`

        """
        if isinstance(exceptions, type):
            exceptions = [ exceptions, ]
        self.exceptions = exceptions
        self.exceptions_dict = dict((e.code, e) for e in self.exceptions)

    def gen_exception (self, err):
        """
        Generates an exception based on a general ProtocolError exception object `err`. 

        When TRex is reserved, no other user can start new TRex runs.

                
        :parameters:
        
         err : exception
            a ProtocolError exception raised by :class:`trex_client.CTRexClient` class

        :return: 
         A TRex exception from the exception list defined in class creation.

         If such exception wasn't found, returns a TRexException exception

        """
        code, message, data = err
        try:
            exp = self.exceptions_dict[code]
            return exp(exp.code, message, data)
        except KeyError:
            # revert to TRexException when unknown error application raised
             return TRexException(err)


exception_handler = CExceptionHandler( EXCEPTIONS )