def _eval_expression(expression, value): """Evaluate an expression for a given value. In case of an error in the execution in the evaluation of the expression, the safe_eval function from Odoo raises ValueError. This method adds more context to the exception raised. :param expression: the expression to eval. :param value: the value to assign to the `value` attribute of the expression. """ try: return restricted_safe_eval(expression, {'value': value}) except ValueError as err: error_message = _( 'The following expression could not be evaluated with the value {value}. ' '\n\n{expression}' '\n\n{err}').format(value=value, expression=expression, err=err) pycompat.reraise( KarmaConditionEvaluationError, KarmaConditionEvaluationError(error_message), sys.exc_info()[2], )
def convert_exception_to(to_type, with_message=False): """ Should only be called from an exception handler. Fetches the current exception data from sys.exc_info() and creates a new exception of type ``to_type`` with the original traceback. If ``with_message`` is ``True``, sets the new exception's message to be the stringification of the original exception. If ``False``, does not set the new exception's message. Otherwise, uses ``with_message`` as the new exception's message. :type with_message: str|bool """ etype, original, tb = sys.exc_info() try: if with_message is False: message = None elif with_message is True: message = str(original) else: message = str(with_message) raise pycompat.reraise(to_type, to_type(message), tb) except to_type as e: return e
def convert_exception_to(to_type, with_message=False): """ Should only be called from an exception handler. Fetches the current exception data from sys.exc_info() and creates a new exception of type ``to_type`` with the original traceback. If ``with_message`` is ``True``, sets the new exception's message to be the stringification of the original exception. If ``False``, does not set the new exception's message. Otherwise, uses ``with_message`` as the new exception's message. :type with_message: str|bool """ etype, original, tb = sys.exc_info() try: if with_message is False: message = None elif with_message is True: message = str(original) else: message = str(with_message) raise pycompat.reraise(to_type, to_type(message), tb) except to_type as e: return e