import psutil


def _interpret_errors_0(error):
    """
    Handle match on SCAE .*  DBE
      where:
         SCAE is StratisCliActionError
         DBE is dbus.exceptions.DBusException

    :param error: a specific error
    :type error: dbus.exceptions.DBusException
    :returns: the interpretation of the error if found, otherwise None
    :rtype: None or str
    """

    # The permissions with which stratis-cli makes requests on the D-Bus
    # are controlled by the "stratisd.conf" file. The CLI tests do not
    # control the contents or installation of "stratisd.conf"
    # and therefore, we cannot test this case reliably.
    if (
        error.get_dbus_name() == "org.freedesktop.DBus.Error.AccessDenied"
    ):  # pragma: no cover
        return (
            "Most likely stratis has insufficient permissions for the action requested."
        )

    # We have observed three causes of this problem. The first is that
    # stratisd is not running at all. The second is that stratisd has not
    # yet established its D-Bus service. The third is that stratisd is
    # running with a new major version and is supplying a different name on the
    # D-Bus than stratis is attempting to use. The second and third
    # possibilities are both covered by a single error message.
    if error.get_dbus_name() == "org.freedesktop.DBus.Error.NameHasNoOwner":
        try:
            # pylint: disable=import-outside-toplevel
            # isort: THIRDPARTY

            for proc in psutil.process_iter():
                try:
                    if proc.name() == "stratisd":  # pragma: no cover
                        return (
                            "Most likely stratis is unable to connect to the "
                            "stratisd D-Bus service."
                        )
                except psutil.NoSuchProcess:  # pragma: no cover
                    pass

            return "It appears that there is no stratisd process running."

        except ImportError:  # pragma: no cover
            return "Most likely there is no stratisd process running."

    # Due to the uncertain behavior with which libdbus
    # treats a timeout value of 0, it proves difficult to test this case,
    # as seen here: https://github.com/stratis-storage/stratis-cli/pull/476
    # Additional information may be found in the issue filed against libdbus
    # here: https://gitlab.freedesktop.org/dbus/dbus/issues/293
    if (
        error.get_dbus_name() == "org.freedesktop.DBus.Error.NoReply"
    ):  # pragma: no cover
        return (
            "stratis attempted communication with the daemon, stratisd, "
            "over the D-Bus, but stratisd did not respond in the allowed time."
        )

    # The goal is to have an explanation for every type of D-Bus error that
    # is encountered. If there is none, then this will rapidly be fixed, so it
    # will be unnecessary to maintain coverage for this branch.
    return None