1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010 EDF R&D
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.
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
23 __date__ ="$31 mars 2010 11:59:33$"
25 from enumerate import Enumerate
27 class UiException(Exception):
30 'USER', # This type should be displayed to end user using a dialog box
31 'ADMIN', # This type should be displayed to admin user in console log
32 'DEVEL' # This type should be displayed to developer only
35 _UImessage = "An error occurs"
39 This exception should be used for functionnal error management, at least in the GUI
40 part of the application, for example to set user oriented messages at point
42 WARN: The exception should NOT be used to hide defaults in the algorithm, but
43 only predicted error in the specified use case.
45 def __init__(self, message, type=TYPES.USER):
46 """Canonical constructor"""
47 Exception.__init__(self,message)
48 self.setUIMessage(message)
51 def setUIMessage(self, UImessage):
52 self._UImessage = UImessage
54 def getUIMessage(self):
55 return self._UImessage
57 def setType(self, type):
58 """Specify the type of this exception. To be choosen in the TYPES list."""
59 if not self.TYPES.isValid(type):
60 raise UiException("The exception type "+str(type)+" can't be used",self.TYPES.DEVEL)
67 return self.getUIMessage()
70 def UserException(msg):
71 return UiException(msg,UiException.TYPES.USER)
73 def AdminException(msg):
74 return UiException(msg,UiException.TYPES.ADMIN)
76 def DevelException(msg):
77 return UiException(msg,UiException.TYPES.DEVEL)
80 # ==============================================================================
81 # Basic use cases and unit test functions
82 # ==============================================================================
84 def somethingGoesWrong():
85 raise UiException("Something goes wrong")
91 except UiException, err:
92 print 'ERROR: %s' % str(err)
93 if ( str(err) == "Something goes wrong" ):
98 def TEST_specificException():
99 print DevelException("err")
100 print AdminException("err")
101 print UserException("err")
104 if __name__ == "__main__":
106 unittester.run("uiexception","TEST_uimessage")
107 unittester.run("uiexception","TEST_specificException")