Back to index

salome-kernel  6.5.0
uiexception.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 __author__="gboulant"
00021 __date__ ="$31 mars 2010 11:59:33$"
00022 
00023 from enumerate import Enumerate
00024 
00025 class UiException(Exception):
00026 
00027     TYPES = Enumerate([
00028         'USER',  # This type should be displayed to end user using a dialog box
00029         'ADMIN', # This type should be displayed to admin user in console log
00030         'DEVEL'  # This type should be displayed to developer only
00031     ])
00032 
00033     _UImessage = "An error occurs"
00034     _type = TYPES.USER
00035 
00036     """
00037     This exception should be used for functionnal error management, at least in the GUI
00038     part of the application, for example to set user oriented messages at point
00039     of exception raise.
00040     WARN: The exception should NOT be used to hide defaults in the algorithm, but
00041     only predicted error in the specified use case.
00042     """
00043     def __init__(self, message, type=TYPES.USER):
00044         """Canonical constructor"""
00045         Exception.__init__(self,message)
00046         self.setUIMessage(message)
00047         self.setType(type)
00048 
00049     def setUIMessage(self, UImessage):
00050         self._UImessage = UImessage
00051 
00052     def getUIMessage(self):
00053         return self._UImessage
00054 
00055     def setType(self, type):
00056         """Specify the type of this exception. To be choosen in the TYPES list."""
00057         if not self.TYPES.isValid(type):
00058             raise UiException("The exception type "+str(type)+" can't be used",self.TYPES.DEVEL)
00059         self._type = type
00060 
00061     def getType(self):
00062         return self._type
00063 
00064     def __str__(self):
00065         return self.getUIMessage()
00066 
00067 
00068 def UserException(msg):
00069     return UiException(msg,UiException.TYPES.USER)
00070 
00071 def AdminException(msg):
00072     return UiException(msg,UiException.TYPES.ADMIN)
00073 
00074 def DevelException(msg):
00075     return UiException(msg,UiException.TYPES.DEVEL)
00076 
00077 #
00078 # ==============================================================================
00079 # Basic use cases and unit test functions
00080 # ==============================================================================
00081 #
00082 def somethingGoesWrong():
00083     raise UiException("Something goes wrong")
00084 
00085 def TEST_uimessage():
00086     try:
00087         somethingGoesWrong()
00088         return False
00089     except UiException, err:
00090         print 'ERROR: %s' % str(err)
00091         if ( str(err) == "Something goes wrong" ):
00092             return True
00093         else:
00094             return False
00095 
00096 def TEST_specificException():
00097     print DevelException("err")
00098     print AdminException("err")
00099     print UserException("err")
00100     return True
00101 
00102 if __name__ == "__main__":
00103     import unittester
00104     unittester.run("uiexception","TEST_uimessage")
00105     unittester.run("uiexception","TEST_specificException")
00106