Trending: Partially remove 3n-hsw
[csit.git] / resources / tools / presentation / pal_errors.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Implementation of exceptions used in the Presentation and analytics layer.
15 """
16
17 import sys
18 import logging
19
20
21 class PresentationError(Exception):
22     """Exception(s) raised by the presentation module.
23
24     When raising this exception, put this information to the message in this
25     order:
26      - short description of the encountered problem (parameter msg),
27      - relevant messages if there are any collected, e.g., from caught
28        exception (optional parameter details),
29      - relevant data if there are any collected (optional parameter details).
30     """
31
32     log_level = {
33         u"DEBUG": logging.debug,
34         u"INFO": logging.info,
35         u"WARNING": logging.warning,
36         u"ERROR": logging.error,
37         u"CRITICAL": logging.critical
38     }
39
40     def __init__(self, msg, details=u'', level=u"CRITICAL"):
41         """Sets the exception message and the level.
42
43         :param msg: Short description of the encountered problem.
44         :param details: Relevant messages if there are any collected, e.g.,
45             from caught exception (optional parameter details), or relevant data
46             if there are any collected (optional parameter details).
47         :param level: Level of the error, possible choices are: "DEBUG", "INFO",
48             "WARNING", "ERROR" and "CRITICAL".
49         :type msg: str
50         :type details: str
51         :type level: str
52         """
53
54         super(PresentationError, self).__init__()
55         self._msg = msg
56         self._details = details
57         self._level = level
58
59         try:
60             self.log_level[self._level](self._msg)
61             if self._details:
62                 self.log_level[self._level](self._details)
63         except KeyError:
64             print(u"Wrong log level.")
65             sys.exit(1)
66
67     def __repr__(self):
68         return (
69             f"PresentationError(msg={self._msg!r},details={self._details!r},"
70             f"level={self._level!r})"
71         )
72
73     def __str__(self):
74         return str(self._msg)
75
76     @property
77     def level(self):
78         """Getter - logging level.
79
80         :returns: Logging level.
81         :rtype: str
82         """
83         return self._level