#!/usr/bin/python # Copyright (c) 2005-2006 by Mark T. Holder # Florida State University. (see end of file) # New file (post April, 2006, expected to be clean under pylint) '''CIPRES emits fairly generic exceptions (because IDL lacks inheritance for exceptions). The code in this module is intended to allow python client and server code to raise and catch richer exceptions easily. The hierarchy hasn't been designed yet. So I'm still using CipresExceptionas a translator. If raised by a servant PIPRes.wrap.impl will translate these to the appropriate type, which is CipresIDL_api1.ApplicationException for most (soon to be all CIPRES calls).''' class CipresException(Exception): # should be in IDL "Exception class that takes major, minor error codes and a message" # MAJOR Error Codes MAJOR_BAD_ARG = 0 MAJOR_BAD_RETURN = 1 MAJOR_RUNTIME = 2 MAJOR_API = 3 MAJOR_CONFIGURATION = 4 MAJOR_LAST_ERROR_CODE = 5 #KEEP THIS LAST!!! (for iterating over exception codes # MINOR Error Codes MINOR_GENERIC = 0 # keep this first (we use it as a default in __init__ as 0) MINOR_DATA_MATRIX = 1 MINOR_TREE = 2 MINOR_SCORE = 3 MINOR_LAST_ERROR_CODE = 4 #KEEP THIS LAST!!! (for iterating over exception codes def __init__(self, majorCode, minorCode = 0, message=''): self.majorCode = majorCode self.message = message self.minorCode = minorCode Exception.__init__(self, self.__str__()) def __str__(self): return '%s:%s:%s' % (majorErrCodeToStr(self.majorCode), minorErrCodeToStr(self.minorCode), self.message) _MAJ_ERR_CODE_TO_STR = { CipresException.MAJOR_BAD_ARG : 'BadArgument', CipresException.MAJOR_BAD_RETURN : 'BadReturnValue', CipresException.MAJOR_RUNTIME : 'GenericRuntimeError', CipresException.MAJOR_CONFIGURATION : 'CipresConfigurationError', CipresException.MAJOR_API : 'APIError', } _MIN_ERR_CODE_TO_STR = { CipresException.MINOR_DATA_MATRIX : 'Matrix', CipresException.MINOR_TREE : 'Tree', CipresException.MINOR_SCORE : 'Score', CipresException.MINOR_GENERIC : '', } def majorErrCodeToStr(err): "translates CipresException.MAJOR_* codes to strings" return _MAJ_ERR_CODE_TO_STR.get(err, 'UnknownMajorErrCode') def minorErrCodeToStr(err): "translates CipresException.MINOR_* codes to strings" return _MIN_ERR_CODE_TO_STR.get(err,'UnknownMinorErrCode') def raisePyExcepFromCipresExcep(ex): "hook for generating the appropriate type of exception to be raised on the client side" assert(isinstance(ex, CipresException)) raise CipresException, ex def raiseCipresExcepFromPyExcep(ex): "hook for translating a PIPRes exception to CIPRES exception (to be passed to the ORB)." if ex.__class__ == CipresException: raise CipresException, ex return raiseCipresExcepFromUnknownErr() def raiseCipresExcepFromUnknownErr(): "WARNING calling sys.exit for debugging purposes." import traceback, cStringIO, sys s = cStringIO.StringIO() traceback.print_exc(None, s) sys.exit(s.getvalue()) if __name__ == '__main__': import doctest doctest.testmod() # This file is part of the PIPRes library # # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA