Repository: Netflix/consoleme
File: consoleme/handlers/policies.py (Python)

Code before:
            log_data["function"], tags={"user": self.user, "ip": self.ip, "arn": arn}
        )

        role = await aws.fetch_iam_role(account_id, arn)

        parsed_policy_change = await parse_policy_change_request(
            self.user, arn, role, data_list
        )

        if parsed_policy_change["status"] == "error":
            await write_json_error(json.dumps(parsed_policy_change), obj=self)
            return

        events = parsed_policy_change["events"]
        policy_status = "pending"
        should_auto_approve_request = await should_auto_approve_policy(
            events, self.user, self.groups
        )
        if should_auto_approve_request is not False:
            policy_status = "approved"
        try:
            resource_actions = await get_resources_from_events(events)
            resources = list(resource_actions.keys())
            resource_policies = await get_resource_policies(arn, resource_actions, account_id)
        except Exception:
            log.error('Something Patrick did blew up!', exc_info=True)
            resource_actions = {}
            resources = []
            resource_policies = {}
        dynamo = UserDynamoHandler(self.user)
        request = await dynamo.write_policy_request(
            self.user, justification, arn, policy_name, events, resources
        )
        if policy_status == "approved":
            try:
                formatted_policy_changes = await get_formatted_policy_changes(
                    account_id, arn, request
                )
            except Exception as e:
                await write_json_error(e, obj=self)
                await self.finish()
                return
            original_policy_document = formatted_policy_changes["changes"][0]["old"]
            request["old_policy"] = json.dumps([original_policy_document])
            result: dict = await update_role_policy(events)
            if result["status"] == "success":
                request["status"] = "approved"
                request[
                    "updated_by"
                ] = f"Auto-Approve Probe: {should_auto_approve_request['approving_probe']}"
                # if approved, Make sure current policy is the same as the one the user thinks they are updating

Review comment (suggestion):
Can you log this as part of the log_data dictionary? 
```
except Exception as e:
    log_data["error"] = e
    log.error(log_data, exc_info=True)
```

Code after:
            log_data["function"], tags={"user": self.user, "ip": self.ip, "arn": arn}
        )

        role = await aws.fetch_iam_role(account_id, arn)

        parsed_policy_change = await parse_policy_change_request(
            self.user, arn, role, data_list
        )

        if parsed_policy_change["status"] == "error":
            await write_json_error(json.dumps(parsed_policy_change), obj=self)
            return

        events = parsed_policy_change["events"]
        policy_status = "pending"
        should_auto_approve_request = await should_auto_approve_policy(
            events, self.user, self.groups
        )
        if should_auto_approve_request is not False:
            policy_status = "approved"
        try:
            resource_actions = await get_resources_from_events(events)
            resources = list(resource_actions.keys())
            resource_policies = await get_resource_policies(arn, resource_actions, account_id)
        except Exception as e:
            log_data["error"] = e
            log.error(log_data, exc_info=True)
            resource_actions = {}
            resources = []
            resource_policies = {}
        dynamo = UserDynamoHandler(self.user)
        request = await dynamo.write_policy_request(
            self.user, justification, arn, policy_name, events, resources
        )
        if policy_status == "approved":
            try:
                formatted_policy_changes = await get_formatted_policy_changes(
                    account_id, arn, request
                )
            except Exception as e:
                await write_json_error(e, obj=self)
                await self.finish()
                return
            original_policy_document = formatted_policy_changes["changes"][0]["old"]
            request["old_policy"] = json.dumps([original_policy_document])
            result: dict = await update_role_policy(events)
            if result["status"] == "success":
                request["status"] = "approved"
                request[
                    "updated_by"
                ] = f"Auto-Approve Probe: {should_auto_approve_request['approving_probe']}"