Salome HOME
Merge changes from 'master' branch.
[modules/kernel.git] / src / KERNEL_PY / kernel / uiexception.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2016  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, or (at your option) any later version.
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
21 ## \defgroup uiexception uiexception
22 #  \{ 
23 #  \details Exception for user error management
24 #  \}
25
26 __author__="gboulant"
27 __date__ ="$31 mars 2010 11:59:33$"
28
29 from .enumerate import Enumerate
30
31 ## This exception should be used for functionnal error management, at least in the GUI
32 #  part of the application, for example to set user oriented messages at point
33 #  of exception raise.
34 #  \warning The exception should NOT be used to hide defaults in the algorithm, but
35 #  only predicted error in the specified use case.
36 #  \ingroup uiexception
37 class UiException(Exception):
38
39     TYPES = Enumerate([
40         'USER',  # This type should be displayed to end user using a dialog box
41         'ADMIN', # This type should be displayed to admin user in console log
42         'DEVEL'  # This type should be displayed to developer only
43     ])
44
45     _UImessage = "An error occurs"
46     _type = TYPES.USER
47
48     """
49     This exception should be used for functionnal error management, at least in the GUI
50     part of the application, for example to set user oriented messages at point
51     of exception raise.
52     WARN: The exception should NOT be used to hide defaults in the algorithm, but
53     only predicted error in the specified use case.
54     """
55     
56     ## Canonical constructor
57     def __init__(self, message, type=TYPES.USER):
58         """Canonical constructor"""
59         Exception.__init__(self,message)
60         self.setUIMessage(message)
61         self.setType(type)
62
63     def setUIMessage(self, UImessage):
64         self._UImessage = UImessage
65
66     def getUIMessage(self):
67         return self._UImessage
68
69     ## Specify the type of this exception. To be chosen in the TYPES list.
70     def setType(self, type):
71         """Specify the type of this exception. To be chosen in the TYPES list."""
72         if not self.TYPES.isValid(type):
73             raise UiException("The exception type "+str(type)+" can't be used",self.TYPES.DEVEL)
74         self._type = type
75
76     def getType(self):
77         return self._type
78
79     def __str__(self):
80         return self.getUIMessage()
81
82
83 def UserException(msg):
84     return UiException(msg,UiException.TYPES.USER)
85
86 def AdminException(msg):
87     return UiException(msg,UiException.TYPES.ADMIN)
88
89 def DevelException(msg):
90     return UiException(msg,UiException.TYPES.DEVEL)
91
92 #
93 # ==============================================================================
94 # Basic use cases and unit test functions
95 # ==============================================================================
96 #
97 def somethingGoesWrong():
98     raise UiException("Something goes wrong")
99
100 def TEST_uimessage():
101     try:
102         somethingGoesWrong()
103         return False
104     except UiException as err:
105         print('ERROR: %s' % str(err))
106         if ( str(err) == "Something goes wrong" ):
107             return True
108         else:
109             return False
110
111 def TEST_specificException():
112     print(DevelException("err"))
113     print(AdminException("err"))
114     print(UserException("err"))
115     return True
116
117 if __name__ == "__main__":
118     from . import unittester
119     unittester.run("uiexception","TEST_uimessage")
120     unittester.run("uiexception","TEST_specificException")
121