# Buggy code
def __virtual__():
    try:
        if salt.utils.platform.is_proxy() and __opts__["proxy"]["proxytype"] == "fx2":
            return __virtualname__
    except KeyError:
        pass
    return False


# Traceback (KeyError)
==> /var/log/salt/master <==
2018-04-10 16:42:43,610 [salt.loader                                                                              :763 ][CRITICAL][48291] Failed to load grains defined in grain file opt-1.fx2 in function <function fx2 at 0x80dfa5510>, error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 756, in grains
ret = funcs[key]()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 90, in fx2
return _grains()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 72, in _grains
(username, password) = _find_credentials()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 41, in _find_credentials
usernames.append(__pillar__['proxy'].get('admin_username', 'root'))
File "/usr/local/lib/python3.6/site-packages/salt/utils/context.py", line 228, in __getitem__
return self._dict()[key]
KeyError: 'proxy'
2018-04-10 16:42:43,613 [salt.loader                                                                              :763 ][CRITICAL][48291] Failed to load grains defined in grain file opt-1.marathon in function <function marathon at 0x80dfaeb70>, error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 756, in grains
ret = funcs[key]()
File "/usr/local/lib/python3.6/site-packages/salt/grains/marathon.py", line 41, in marathon
"{0}/v2/info".format(__opts__['proxy'].get(
KeyError: 'proxy'

==> /var/log/salt/minion <==
2018-04-10 16:42:48,701 [salt.loader                                                                              :763 ][CRITICAL][60649] Failed to load grains defined in grain file opt-1.fx2 in function <function fx2 at 0x80de467b8>, error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 756, in grains
ret = funcs[key]()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 90, in fx2
return _grains()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 72, in _grains
(username, password) = _find_credentials()
File "/usr/local/lib/python3.6/site-packages/salt/grains/fx2.py", line 41, in _find_credentials
usernames.append(__pillar__['proxy'].get('admin_username', 'root'))
File "/usr/local/lib/python3.6/site-packages/salt/utils/context.py", line 228, in __getitem__
return self._dict()[key]
KeyError: 'proxy'
2018-04-10 16:42:48,703 [salt.loader                                                                              :763 ][CRITICAL][60649] Failed to load grains defined in grain file opt-1.marathon in function <function marathon at 0x80de4de18>, error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 756, in grains
ret = funcs[key]()
File "/usr/local/lib/python3.6/site-packages/salt/grains/marathon.py", line 41, in marathon
"{0}/v2/info".format(__opts__['proxy'].get(
KeyError: 'proxy'

# Fixed code
def __virtual__():
    if (
        salt.utils.platform.is_proxy()
        and "proxy" in __opts__
        and __opts__["proxy"].get("proxytype") == "fx2"
    ):
        return __virtualname__
    return False