1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 ## \defgroup uiexception uiexception
23 # \details Exception for user error management
27 __date__ ="$31 mars 2010 11:59:33$"
29 from enumerate import Enumerate
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
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):
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
45 _UImessage = "An error occurs"
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
52 WARN: The exception should NOT be used to hide defaults in the algorithm, but
53 only predicted error in the specified use case.
56 ## Canonical constructor
57 def __init__(self, message, type=TYPES.USER):
58 """Canonical constructor"""
59 Exception.__init__(self,message)
60 self.setUIMessage(message)
63 def setUIMessage(self, UImessage):
64 self._UImessage = UImessage
66 def getUIMessage(self):
67 return self._UImessage
69 ## Specify the type of this exception. To be choosen in the TYPES list.
70 def setType(self, type):
71 """Specify the type of this exception. To be choosen 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)
80 return self.getUIMessage()
83 def UserException(msg):
84 return UiException(msg,UiException.TYPES.USER)
86 def AdminException(msg):
87 return UiException(msg,UiException.TYPES.ADMIN)
89 def DevelException(msg):
90 return UiException(msg,UiException.TYPES.DEVEL)
93 # ==============================================================================
94 # Basic use cases and unit test functions
95 # ==============================================================================
97 def somethingGoesWrong():
98 raise UiException("Something goes wrong")
100 def TEST_uimessage():
104 except UiException, err:
105 print 'ERROR: %s' % str(err)
106 if ( str(err) == "Something goes wrong" ):
111 def TEST_specificException():
112 print DevelException("err")
113 print AdminException("err")
114 print UserException("err")
117 if __name__ == "__main__":
119 unittester.run("uiexception","TEST_uimessage")
120 unittester.run("uiexception","TEST_specificException")