CSIT-1288: Prepare data to be sent by Jenkins
[csit.git] / resources / tools / presentation / errors.py
1 # Copyright (c) 2017 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_exception = {"DEBUG": logging.debug,
33                      "INFO": logging.info,
34                      "WARNING": logging.warning,
35                      "ERROR": logging.error,
36                      "CRITICAL": logging.critical}
37
38     def __init__(self, msg, details='', level="CRITICAL"):
39         """Sets the exception message and the level.
40
41         :param msg: Short description of the encountered problem.
42         :param details: Relevant messages if there are any collected, e.g.,
43         from caught exception (optional parameter details), or relevant data if
44         there are any collected (optional parameter details).
45         :param level: Level of the error, possible choices are: "DEBUG", "INFO",
46         "WARNING", "ERROR" and "CRITICAL".
47         :type msg: str
48         :type details: str
49         :type level: str
50         """
51
52         super(PresentationError, self).__init__()
53         self._msg = msg
54         self._details = details
55         self._level = level
56
57         try:
58             self.log_exception[self._level](self._msg)
59             if self._details:
60                 self.log_exception[self._level](self._details)
61         except KeyError:
62             print("Wrong log level.")
63             sys.exit(1)
64
65     def __repr__(self):
66         return (
67             "PresentationError(msg={msg!r},details={dets!r},level={level!r})".
68             format(msg=self._msg, dets=self._details, level=self._level))
69
70     def __str__(self):
71         return str(self._msg)
72
73     @property
74     def level(self):
75         """Getter - logging level.
76
77         :returns: Logging level.
78         :rtype: str
79         """
80         return self._level