1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2013 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.
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 __date__ ="$31 mars 2010 11:59:33$"
23 from enumerate import Enumerate
25 class UiException(Exception):
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
33 _UImessage = "An error occurs"
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
40 WARN: The exception should NOT be used to hide defaults in the algorithm, but
41 only predicted error in the specified use case.
43 def __init__(self, message, type=TYPES.USER):
44 """Canonical constructor"""
45 Exception.__init__(self,message)
46 self.setUIMessage(message)
49 def setUIMessage(self, UImessage):
50 self._UImessage = UImessage
52 def getUIMessage(self):
53 return self._UImessage
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)
65 return self.getUIMessage()
68 def UserException(msg):
69 return UiException(msg,UiException.TYPES.USER)
71 def AdminException(msg):
72 return UiException(msg,UiException.TYPES.ADMIN)
74 def DevelException(msg):
75 return UiException(msg,UiException.TYPES.DEVEL)
78 # ==============================================================================
79 # Basic use cases and unit test functions
80 # ==============================================================================
82 def somethingGoesWrong():
83 raise UiException("Something goes wrong")
89 except UiException, err:
90 print 'ERROR: %s' % str(err)
91 if ( str(err) == "Something goes wrong" ):
96 def TEST_specificException():
97 print DevelException("err")
98 print AdminException("err")
99 print UserException("err")
102 if __name__ == "__main__":
104 unittester.run("uiexception","TEST_uimessage")
105 unittester.run("uiexception","TEST_specificException")