Salome HOME
CMake: set the flag Boost_NO_BOOST_CMAKE to avoid that the *standard* FindBoost.cmake
[modules/kernel.git] / src / KERNEL_PY / kernel / uiexception.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2013  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 __author__="gboulant"
21 __date__ ="$31 mars 2010 11:59:33$"
22
23 from enumerate import Enumerate
24
25 class UiException(Exception):
26
27     TYPES = Enumerate([
28         'USER',  # This type should be displayed to end user using a dialog box
29         'ADMIN', # This type should be displayed to admin user in console log
30         'DEVEL'  # This type should be displayed to developer only
31     ])
32
33     _UImessage = "An error occurs"
34     _type = TYPES.USER
35
36     """
37     This exception should be used for functionnal error management, at least in the GUI
38     part of the application, for example to set user oriented messages at point
39     of exception raise.
40     WARN: The exception should NOT be used to hide defaults in the algorithm, but
41     only predicted error in the specified use case.
42     """
43     def __init__(self, message, type=TYPES.USER):
44         """Canonical constructor"""
45         Exception.__init__(self,message)
46         self.setUIMessage(message)
47         self.setType(type)
48
49     def setUIMessage(self, UImessage):
50         self._UImessage = UImessage
51
52     def getUIMessage(self):
53         return self._UImessage
54
55     def setType(self, type):
56         """Specify the type of this exception. To be choosen in the TYPES list."""
57         if not self.TYPES.isValid(type):
58             raise UiException("The exception type "+str(type)+" can't be used",self.TYPES.DEVEL)
59         self._type = type
60
61     def getType(self):
62         return self._type
63
64     def __str__(self):
65         return self.getUIMessage()
66
67
68 def UserException(msg):
69     return UiException(msg,UiException.TYPES.USER)
70
71 def AdminException(msg):
72     return UiException(msg,UiException.TYPES.ADMIN)
73
74 def DevelException(msg):
75     return UiException(msg,UiException.TYPES.DEVEL)
76
77 #
78 # ==============================================================================
79 # Basic use cases and unit test functions
80 # ==============================================================================
81 #
82 def somethingGoesWrong():
83     raise UiException("Something goes wrong")
84
85 def TEST_uimessage():
86     try:
87         somethingGoesWrong()
88         return False
89     except UiException, err:
90         print 'ERROR: %s' % str(err)
91         if ( str(err) == "Something goes wrong" ):
92             return True
93         else:
94             return False
95
96 def TEST_specificException():
97     print DevelException("err")
98     print AdminException("err")
99     print UserException("err")
100     return True
101
102 if __name__ == "__main__":
103     import unittester
104     unittester.run("uiexception","TEST_uimessage")
105     unittester.run("uiexception","TEST_specificException")
106