# Buggy code
def execute(self, image_obj, exec_context):
    """
    Execute the trigger specified in the rule with the image and gate (for prepared context) and exec_context)

    :param image_obj: The image to execute against
    :param exec_context: The prepared execution context from the gate init
    :return: a tuple of a list of errors and a list of PolicyRuleDecisions, one for each fired trigger match produced by the trigger execution
    """

    matches = None

    try:
        if not self.configured_trigger:
            log.error(
                "No configured trigger to execute for gate {} and trigger: {}. Returning".format(
                    self.gate_name, self.trigger_name
                )
            )
            raise TriggerNotFoundError(
                trigger_name=self.trigger_name, gate_name=self.gate_name
            )

        if self.gate_cls.__lifecycle_state__ == LifecycleStates.eol:
            self.errors.append(
                EndOfLifedError(
                    gate_name=self.gate_name, superceded=self.gate_cls.__superceded_by__
                )
            )
        elif self.gate_cls.__lifecycle_state__ == LifecycleStates.deprecated:
            self.errors.append(
                DeprecationWarning(
                    gate_name=self.gate_name, superceded=self.gate_cls.__superceded_by__
                )
            )
        elif self.configured_trigger.__lifecycle_state__ == LifecycleStates.eol:
            self.errors.append(
                EndOfLifedError(
                    gate_name=self.gate_name,
                    trigger_name=self.trigger_name,
                    superceded=self.configured_trigger.__superceded_by__,
                )
            )
        elif self.configured_trigger.__lifecycle_state__ == LifecycleStates.deprecated:
            self.errors.append(
                DeprecationWarning(
                    gate_name=self.gate_name,
                    trigger_name=self.trigger_name,
                    superceded=self.configured_trigger.__superceded_by__,
                )
            )

        try:
            self.configured_trigger.execute(image_obj, exec_context)
        except TriggerEvaluationError:
            raise
        except Exception as e:
            log.exception("Unmapped exception caught during trigger evaluation")
            raise TriggerEvaluationError(
                trigger=self.configured_trigger,
                message="Could not evaluate trigger due to error in evaluation execution",
            )

        matches = self.configured_trigger.fired
        decisions = []

        # Try all rules and record all decisions and errors so multiple errors can be reported if present, not just the first encountered
        for match in matches:
            try:
                decisions.append(
                    PolicyRuleDecision(trigger_match=match, policy_rule=self)
                )
            except TriggerEvaluationError as e:
                log.exception("Policy rule decision mapping exception: {}".format(e))
                self.errors.append(str(e))

        return self.errors, decisions
    except Exception as e:
        log.exception(
            "Error executing trigger {} on image {}".format(
                self.trigger_name, image_obj.id
            )
        )
        raise


# Traceback (ValueError)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] Traceback (most recent call last):
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/gate.py", line 234, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     self.evaluate(image_obj, context)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/gates/image_metadata.py", line 59, in evaluate
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     rval = type(img_val)(rval)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] ValueError: invalid literal for int() with base 10: '4gb'
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] During handling of the above exception, another exception occurred:
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] Traceback (most recent call last):
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/bundles.py", line 453, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     self.configured_trigger.execute(image_obj, exec_context)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/gate.py", line 237, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     raise TriggerEvaluationError(trigger=self, message='Error executing gate {} trigger {} with params: {}. Msg: {}'.format(self.gate_cls.__gate_name__, self.__trigger_name__, self.eval_params, e.message))
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] AttributeError: 'ValueError' object has no attribute 'message'
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] During handling of the above exception, another exception occurred:
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] Traceback (most recent call last):
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/api/controllers/synchronous_operations.py", line 481, in check_user_image_inline
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     eval_result = executable_bundle.execute(img_obj, tag, ExecutionContext(db_session=db, configuration={}))
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/bundles.py", line 1260, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     bundle_exec = self._process_mapping_result(bundle_exec, image_object, tag, context)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/bundles.py", line 1207, in _process_mapping_result
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     errors, policy_decision = evaluated_policy.execute(image_obj=image_object, context=context)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/bundles.py", line 596, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     errs, matches = rule.execute(image_obj=image_obj, exec_context=exec_context)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/engine/policy/bundles.py", line 458, in execute
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     raise TriggerEvaluationError(trigger=self.configured_trigger, message='Could not evaluate trigger due to error in evaluation execution')
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] anchore_engine.services.policy_engine.engine.policy.exceptions.TriggerEvaluationError: TriggerEvaluationError: severity:error message:Trigger evaluation failed for gate metadata and trigger attribute, with parameters: ([]) due to: Could not evaluate trigger due to error in evaluation execution
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] During handling of the above exception, another exception occurred:
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-] Traceback (most recent call last):
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/apis/authorization.py", line 339, in inner_wrapper
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     return f(*args, **kwargs)
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/anchore_engine/services/policy_engine/api/controllers/synchronous_operations.py", line 484, in check_user_image_inline
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     abort(Response(response='Cannot execute given policy against the image due to errors executing the policy bundle: {}'.format(e.message), status=500))
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]   File "/usr/local/lib/python3.6/dist-packages/werkzeug/exceptions.py", line 707, in abort
engine-policy-engine_1  | [service:policy-engine] 2019-01-11 05:49:12+0000 [-]     return _aborter(status, *args, **kwargs)

# Fixed code
def execute(self, image_obj, exec_context):
    """
    Execute the trigger specified in the rule with the image and gate (for prepared context) and exec_context)

    :param image_obj: The image to execute against
    :param exec_context: The prepared execution context from the gate init
    :return: a tuple of a list of errors and a list of PolicyRuleDecisions, one for each fired trigger match produced by the trigger execution
    """

    matches = None

    try:
        if not self.configured_trigger:
            log.error(
                "No configured trigger to execute for gate {} and trigger: {}. Returning".format(
                    self.gate_name, self.trigger_name
                )
            )
            raise TriggerNotFoundError(
                trigger_name=self.trigger_name, gate_name=self.gate_name
            )

        if self.gate_cls.__lifecycle_state__ == LifecycleStates.eol:
            self.errors.append(
                EndOfLifedError(
                    gate_name=self.gate_name, superceded=self.gate_cls.__superceded_by__
                )
            )
        elif self.gate_cls.__lifecycle_state__ == LifecycleStates.deprecated:
            self.errors.append(
                DeprecationWarning(
                    gate_name=self.gate_name, superceded=self.gate_cls.__superceded_by__
                )
            )
        elif self.configured_trigger.__lifecycle_state__ == LifecycleStates.eol:
            self.errors.append(
                EndOfLifedError(
                    gate_name=self.gate_name,
                    trigger_name=self.trigger_name,
                    superceded=self.configured_trigger.__superceded_by__,
                )
            )
        elif self.configured_trigger.__lifecycle_state__ == LifecycleStates.deprecated:
            self.errors.append(
                DeprecationWarning(
                    gate_name=self.gate_name,
                    trigger_name=self.trigger_name,
                    superceded=self.configured_trigger.__superceded_by__,
                )
            )

        try:
            self.configured_trigger.execute(image_obj, exec_context)
        except TriggerEvaluationError:
            raise
        except Exception as e:
            log.exception("Unmapped exception caught during trigger evaluation")
            raise TriggerEvaluationError(
                trigger=self.configured_trigger, message="Could not evaluate trigger"
            )

        matches = self.configured_trigger.fired
        decisions = []

        # Try all rules and record all decisions and errors so multiple errors can be reported if present, not just the first encountered
        for match in matches:
            try:
                decisions.append(
                    PolicyRuleDecision(trigger_match=match, policy_rule=self)
                )
            except TriggerEvaluationError as e:
                log.exception("Policy rule decision mapping exception: {}".format(e))
                self.errors.append(str(e))

        return self.errors, decisions
    except Exception as e:
        log.exception(
            "Error executing trigger {} on image {}".format(
                self.trigger_name, image_obj.id
            )
        )
        raise