john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

oxygen exception exit decorator

# 2013-03-06 johnpfeiffer

from functools import wraps
import o2lib
import sys

def oxygen_exception_exit( logger = None ):
    """ If an Oxygen Exception or other fatal exception is raised log and exit the application
    :logger: logger to use. If None, print
    """
    def deco_oxygen_exception_exit( f ):

        @wraps( f )
        def f_oxygen_exception_exit( *args , **kwargs ):
            try:
                return f( *args , **kwargs )

            except o2lib.ApiRuleException as error :
                error_detail = o2lib.ApiRuleErrorCode._VALUES_TO_NAMES[ error.errorCode ]
                message = '%s, please check that your api key is valid and try again. %s' % ( str( error_detail ), str(error ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

            except o2lib.ApiSystemsException as error :
                error_detail = o2lib.ApiSystemsErrorCode._VALUES_TO_NAMES[ error.errorCode ]
                message = '%s, please check that you have entered the correct username and password. %s' % ( str( error_detail ), str(error ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

            except o2lib.ApiInvalidInputException as error :
                error_detail = o2lib.ApiInvalidInputErrorCode._VALUES_TO_NAMES[ error.errorCode ]
                message = '%s, please check that you have entered the correct username and password. %s' % ( str( error_detail ), str(error ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

            except IOError as error :
                message = 'please verify network connectivity and that %s is the correct url, IOERROR: %s' % ( str( error ) , repr( args ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

            except EOFError as error :
                message = 'ERROR: invalid response from the target server, please verify the url %s' % ( repr( args ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

            except AttributeError as error:               # print a summary error and log the whole stack trace
                message = 'ERROR: invalid response from the target server %s, please try running the application again' % ( repr( args ) )
                if logger:
                    logger.error( message )
                print message
                sys.exit( 1 )

        return f_oxygen_exception_exit  # true decorator

    return deco_oxygen_exception_exit

  • « retry decorator test
  • o2lib »

Published

Mar 9, 2013

Category

python-oxygencloud-snapshot

~248 words

Tags

  • decorator 8
  • exception 4
  • exit 1
  • oxygen 14
  • python 180
  • snapshot 12