1"""
2Bareos specific exceptions
3"""
4
5from bareos.bsock.constants import Constants
6
7
8class Error(Exception):
9    """
10    general error exception
11    """
12
13    pass
14
15
16class ConnectionError(Error):
17    """
18    error with the Connection
19    """
20
21    pass
22
23
24class ConnectionLostError(Error):
25    """
26    error with the Connection
27    """
28
29    pass
30
31
32class SocketEmptyHeader(Error):
33    """
34    socket connection received an empty header. Connection lost?
35    """
36
37    pass
38
39
40class AuthenticationError(ConnectionError):
41    """
42    error during Authentication
43    """
44
45    pass
46
47
48class PamAuthenticationError(AuthenticationError):
49    """
50    error during PAM Authentication
51    """
52
53    pass
54
55
56class SignalReceivedException(Error):
57    """
58    received a signal during a connection.
59    """
60
61    def __init__(self, signal):
62        # Call the base class constructor with the parameters it needs
63        message = Constants.get_description(signal)
64        super(SignalReceivedException, self).__init__(
65            "{0} ({1})".format(message, signal)
66        )
67
68        # Now for your custom code...
69        self.signal = signal
70
71
72class JsonRpcErrorReceivedException(Error):
73    """
74    This exception is raised,
75    if a JSON-RPC error object is received.
76    """
77
78    def __init__(self, jsondata):
79        # Call the base class constructor with the parameters it needs
80
81        # Expected result will look like this:
82        #
83        # {u'jsonrpc': u'2.0', u'id': None, u'error': {u'message': u'failed', u'code': 1, u'data': {u'messages': {u'error': [u'INVALIDCOMMAND: is an invalid command.\n']}, u'result': {}}}}
84
85        try:
86            message = jsondata["error"]["message"]
87        except KeyError:
88            message = ""
89        try:
90            errormessages = jsondata["error"]["data"]["messages"]["error"]
91            error = "".join(errormessages)
92        except (KeyError, TypeError):
93            error = str(jsondata)
94
95        super(JsonRpcErrorReceivedException, self).__init__(
96            "{0}: {1}".format(message, error)
97        )
98
99        # Now for your custom code...
100        self.jsondata = jsondata
101
102
103class JsonRpcInvalidJsonReceivedException(JsonRpcErrorReceivedException):
104    """
105    This exception is raised,
106    if a invalid JSON-RPC object is received (e.g. data is not a valid JSON structure).
107    """
108
109    def __init__(self, jsondata):
110        # Call the base class constructor with the parameters it needs
111
112        # Expected result will look like this:
113        #
114        # {'error': {'message': 'No JSON object could be decoded', 'code': 2, 'data': bytearray(b'Client {\n  Name = "bareos-fd"\n  Description = "Client resource of the Director itself."\n  Address = "localhost"\n  Password = "****************"\n}\n\n{"jsonrpc":"2.0","id":null,"result":{}}')}}
115
116        try:
117            message = jsondata["error"]["message"]
118        except KeyError:
119            message = ""
120        try:
121            origdata = str(jsondata["error"]["data"])
122        except (KeyError, TypeError):
123            origdata = ""
124
125        super(JsonRpcErrorReceivedException, self).__init__(
126            "{0}: {1}".format(message, origdata)
127        )
128
129        # Now for your custom code...
130        self.jsondata = jsondata
131