# Buggy code
def to_serializable_dict(self, mask_secrets=False):
    """
    Serialize database model to a dictionary.

    :param mask_secrets: True to mask secrets in the resulting dict.
    :type mask_secrets: ``boolean``

    :rtype: ``dict``
    """
    serializable_dict = {}
    for k in sorted(six.iterkeys(self._fields)):
        v = getattr(self, k)
        v = str(v) if isinstance(v, JSON_UNFRIENDLY_TYPES) else v
        serializable_dict[k] = v

    if mask_secrets and cfg.CONF.log.mask_secrets:
        serializable_dict = self.mask_secrets(value=serializable_dict)

    return serializable_dict


# Traceback (AttributeError)
2020-05-04 14:18:49,473 140709056184880 INFO engine [-] Found 1 rules defined for trigger core.38d1bbdd-a659-4038-b3f4-ce250eb79db8
2020-05-04 14:18:49,475 140709056184880 ERROR traceback [-] Traceback (most recent call last):

2020-05-04 14:18:49,476 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit

2020-05-04 14:18:49,476 140709056184880 ERROR traceback [-]     msg = self.format(record)

2020-05-04 14:18:49,477 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format

2020-05-04 14:18:49,477 140709056184880 ERROR traceback [-]     return fmt.format(record)

2020-05-04 14:18:49,478 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 176, in format

2020-05-04 14:18:49,478 140709056184880 ERROR traceback [-]     attributes = self._format_extra_attributes(attributes=attributes)

2020-05-04 14:18:49,479 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 151, in _format_extra_attributes

2020-05-04 14:18:49,480 140709056184880 ERROR traceback [-]     value = serialize_object(obj=value)

2020-05-04 14:18:49,480 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 65, in serialize_object

2020-05-04 14:18:49,481 140709056184880 ERROR traceback [-]     value = obj.to_serializable_dict(mask_secrets=True)

2020-05-04 14:18:49,481 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/stormbase.py", line 105, in to_serializable_dict

2020-05-04 14:18:49,482 140709056184880 ERROR traceback [-]     serializable_dict = self.mask_secrets(value=serializable_dict)

2020-05-04 14:18:49,482 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/rule.py", line 122, in mask_secrets

2020-05-04 14:18:49,483 140709056184880 ERROR traceback [-]     action_ref = result.get('action', {}).get('ref', None)

2020-05-04 14:18:49,484 140709056184880 ERROR traceback [-] AttributeError: 'str' object has no attribute 'get'

2020-05-04 14:18:49,484 140709056184880 ERROR filter [-] Logged from file filter.py, line 72

2020-05-04 14:18:49,486 140709056184880 ERROR traceback [-] Traceback (most recent call last):

2020-05-04 14:18:49,487 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit

2020-05-04 14:18:49,488 140709056184880 ERROR traceback [-]     msg = self.format(record)

2020-05-04 14:18:49,488 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format

2020-05-04 14:18:49,489 140709056184880 ERROR traceback [-]     return fmt.format(record)

2020-05-04 14:18:49,489 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 176, in format

2020-05-04 14:18:49,490 140709056184880 ERROR traceback [-]     attributes = self._format_extra_attributes(attributes=attributes)

2020-05-04 14:18:49,490 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 151, in _format_extra_attributes

2020-05-04 14:18:49,491 140709056184880 ERROR traceback [-]     value = serialize_object(obj=value)

2020-05-04 14:18:49,491 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 65, in serialize_object

2020-05-04 14:18:49,492 140709056184880 ERROR traceback [-]     value = obj.to_serializable_dict(mask_secrets=True)

2020-05-04 14:18:49,493 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/stormbase.py", line 105, in to_serializable_dict

2020-05-04 14:18:49,493 140709056184880 ERROR traceback [-]     serializable_dict = self.mask_secrets(value=serializable_dict)

2020-05-04 14:18:49,494 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/rule.py", line 122, in mask_secrets

2020-05-04 14:18:49,494 140709056184880 ERROR traceback [-]     action_ref = result.get('action', {}).get('ref', None)

2020-05-04 14:18:49,495 140709056184880 ERROR traceback [-] AttributeError: 'str' object has no attribute 'get'

2020-05-04 14:18:49,495 140709056184880 ERROR handlers [-] Logged from file filter.py, line 72

2020-05-04 14:18:49,497 140709056184880 ERROR traceback [-] Traceback (most recent call last):

2020-05-04 14:18:49,498 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit

2020-05-04 14:18:49,499 140709056184880 ERROR traceback [-]     msg = self.format(record)

2020-05-04 14:18:49,499 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format

2020-05-04 14:18:49,500 140709056184880 ERROR traceback [-]     return fmt.format(record)

2020-05-04 14:18:49,500 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 241, in format

2020-05-04 14:18:49,501 140709056184880 ERROR traceback [-]     attributes = self._format_extra_attributes(attributes=attributes)

2020-05-04 14:18:49,501 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 151, in _format_extra_attributes

2020-05-04 14:18:49,502 140709056184880 ERROR traceback [-]     value = serialize_object(obj=value)

2020-05-04 14:18:49,503 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 65, in serialize_object

2020-05-04 14:18:49,503 140709056184880 ERROR traceback [-]     value = obj.to_serializable_dict(mask_secrets=True)

2020-05-04 14:18:49,504 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/stormbase.py", line 105, in to_serializable_dict

2020-05-04 14:18:49,504 140709056184880 ERROR traceback [-]     serializable_dict = self.mask_secrets(value=serializable_dict)

2020-05-04 14:18:49,505 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/rule.py", line 122, in mask_secrets

2020-05-04 14:18:49,505 140709056184880 ERROR traceback [-]     action_ref = result.get('action', {}).get('ref', None)

2020-05-04 14:18:49,506 140709056184880 ERROR traceback [-] AttributeError: 'str' object has no attribute 'get'

2020-05-04 14:18:49,506 140709056184880 ERROR handlers [-] Logged from file filter.py, line 72

2020-05-04 14:18:49,508 140709056184880 INFO matcher [-] 1 rule(s) found to enforce for 38d1bbdd-a659-4038-b3f4-ce250eb79db8.
2020-05-04 14:18:49,508 140709056184880 INFO engine [-] Matched 1 rule(s) for trigger_instance 5eb05c896077d5bca5610507 (trigger=core.38d1bbdd-a659-4038-b3f4-ce250eb79db8)
2020-05-04 14:18:49,524 140709056184880 INFO enforcer [-] Invoking action core.local for trigger_instance 5eb05c896077d5bca5610507 with params {"cmd": "echo \"{{trigger.executed_at}}\""}.
2020-05-04 14:18:49,739 140709056184880 ERROR traceback [-] Traceback (most recent call last):

2020-05-04 14:18:49,740 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit

2020-05-04 14:18:49,740 140709056184880 ERROR traceback [-]     msg = self.format(record)

2020-05-04 14:18:49,741 140709056184880 ERROR traceback [-]   File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format

2020-05-04 14:18:49,741 140709056184880 ERROR traceback [-]     return fmt.format(record)

2020-05-04 14:18:49,742 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 241, in format

2020-05-04 14:18:49,743 140709056184880 ERROR traceback [-]     attributes = self._format_extra_attributes(attributes=attributes)

2020-05-04 14:18:49,743 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 151, in _format_extra_attributes

2020-05-04 14:18:49,744 140709056184880 ERROR traceback [-]     value = serialize_object(obj=value)

2020-05-04 14:18:49,744 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/logging/formatters.py", line 65, in serialize_object

2020-05-04 14:18:49,745 140709056184880 ERROR traceback [-]     value = obj.to_serializable_dict(mask_secrets=True)

2020-05-04 14:18:49,745 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/stormbase.py", line 105, in to_serializable_dict

2020-05-04 14:18:49,746 140709056184880 ERROR traceback [-]     serializable_dict = self.mask_secrets(value=serializable_dict)

2020-05-04 14:18:49,748 140709056184880 ERROR traceback [-]   File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/models/db/rule.py", line 122, in mask_secrets

2020-05-04 14:18:49,748 140709056184880 ERROR traceback [-]     action_ref = result.get('action', {}).get('ref', None)

2020-05-04 14:18:49,749 140709056184880 ERROR traceback [-] AttributeError: 'str' object has no attribute 'get'

2020-05-04 14:18:49,750 140709056184880 ERROR handlers [-] Logged from file enforcer.py, line 118

2020-05-04 14:18:50,419 140709057006000 INFO engine [-] Found 0 rules defined for trigger core.st2.generic.actiontrigger
2020-05-04 14:18:50,424 140709057006000 INFO engine [-] No matching rules found for trigger instance 5eb05c8a6077d5bca561050c.

# Fixed code
def to_serializable_dict(self, mask_secrets=False):
    """
    Serialize database model to a dictionary.

    :param mask_secrets: True to mask secrets in the resulting dict.
    :type mask_secrets: ``boolean``

    :rtype: ``dict``
    """
    serializable_dict = {}
    for k in sorted(six.iterkeys(self._fields)):
        v = getattr(self, k)
        if isinstance(v, JSON_UNFRIENDLY_TYPES):
            v = str(v)
        elif isinstance(v, me.EmbeddedDocument):
            v = json.loads(v.to_json())

        serializable_dict[k] = v

    if mask_secrets and cfg.CONF.log.mask_secrets:
        serializable_dict = self.mask_secrets(value=serializable_dict)

    return serializable_dict