# -*- coding: utf-8 -*- """ @file exceptions.py @brief Defines all possible exceptions @par History - Brian Helgans, Dec 17 2019, Created """ import sys import functools from . import log as Log import subprocess class AlgorithmExecutionError(subprocess.CalledProcessError): ''' raised when the executable reports an error ''' def __init__(self, exception = None, message = "unknown"): ''' initialize :param exception: this can be used to save the original exception :type exception: Exception :param message: Describe the exception :type message: str ''' self.caught = exception self.message = message class UserInputException(Exception): ''' raised with for user error and exists to prevent traceback in the pretty print decorator ''' def __init__(self, message): ''' Describes the exception :param message: Describes the exception :type message: str ''' self.message = message def pretty_print_algorithm_execution_error(func): ''' Decorator to catch algorithm execution errors. We don't want traceback in python when algorithm execution dies. :param func: function :type func: func ''' @functools.wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except (AlgorithmExecutionError, UserInputException) as e: sys.tracebacklimit = 0 Log.fatal("main", "Stopped executing because of a failure: "+e.message) sys.exit(1) return wrapper