# Buggy code
def highstate(test=None, **kwargs):
    """
    Retrieve the state data from the salt master for this minion and execute it

    CLI Example:

    .. code-block:: bash

        salt '*' state.highstate

        salt '*' state.highstate exclude=sls_to_exclude
        salt '*' state.highstate exclude="[{'id': 'id_to_exclude'}, {'sls': 'sls_to_exclude'}]"
    """
    __pillar__.update(kwargs.get("pillar", {}))
    st_kwargs = __salt__.kwargs
    __opts__["grains"] = __grains__
    opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
    st_ = salt.client.ssh.state.SSHHighState(
        opts, __pillar__, __salt__, __context__["fileclient"]
    )
    st_.push_active()
    chunks = st_.compile_low_chunks()
    file_refs = salt.client.ssh.state.lowstate_file_refs(
        chunks,
        _merge_extra_filerefs(
            kwargs.get("extra_filerefs", ""), opts.get("extra_filerefs", "")
        ),
    )
    # Check for errors
    for chunk in chunks:
        if not isinstance(chunk, dict):
            __context__["retcode"] = 1
            return chunks

    roster = salt.roster.Roster(opts, opts.get("roster", "flat"))
    roster_grains = roster.opts["grains"]

    # Create the tar containing the state pkg and relevant files.
    _cleanup_slsmod_low_data(chunks)
    trans_tar = salt.client.ssh.state.prep_trans_tar(
        opts,
        __context__["fileclient"],
        chunks,
        file_refs,
        __pillar__,
        st_kwargs["id_"],
        roster_grains,
    )
    trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"])
    cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format(
        opts["thin_dir"], test, trans_tar_sum, opts["hash_type"]
    )
    single = salt.client.ssh.Single(
        opts,
        cmd,
        fsclient=__context__["fileclient"],
        minion_opts=__salt__.minion_opts,
        **st_kwargs,
    )
    single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"]))
    stdout, stderr, _ = single.cmd_block()

    # Clean up our tar
    try:
        os.remove(trans_tar)
    except (OSError, IOError):
        pass

    # Read in the JSON data and return the data structure
    try:
        return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
    except Exception as e:
        log.error("JSON Render failed for: %s\n%s", stdout, stderr)
        log.error(six.text_type(e))

    # If for some reason the json load fails, return the stdout
    return stdout


# Traceback (KeyError)
ssh:
----------
ID: echo
Function: cmd.run
Name: echo 'This is a test!'
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/var/tmp/.root_62a99a_salt/py3/salt/state.py", line 1905, in call
**cdata['kwargs'])
File "/var/tmp/.root_62a99a_salt/py3/salt/loader.py", line 1822, in wrapper
return f(*args, **kwargs)
File "/var/tmp/.root_62a99a_salt/py3/salt/states/cmd.py", line 865, in run
'shell': shell or __grains__['shell'],
File "/var/tmp/.root_62a99a_salt/py3/salt/utils/context.py", line 228, in __getitem__
return self._dict()[key]
KeyError: 'shell'
Started: 19:56:36.251433
Duration: 6.909 ms
Changes:

# Fixed code
def highstate(test=None, **kwargs):
    """
    Retrieve the state data from the salt master for this minion and execute it

    CLI Example:

    .. code-block:: bash

        salt '*' state.highstate

        salt '*' state.highstate exclude=sls_to_exclude
        salt '*' state.highstate exclude="[{'id': 'id_to_exclude'}, {'sls': 'sls_to_exclude'}]"
    """
    __pillar__.update(kwargs.get("pillar", {}))
    st_kwargs = __salt__.kwargs
    __opts__["grains"] = __grains__
    opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
    st_ = salt.client.ssh.state.SSHHighState(
        opts, __pillar__, __salt__, __context__["fileclient"]
    )
    st_.push_active()
    chunks = st_.compile_low_chunks()
    file_refs = salt.client.ssh.state.lowstate_file_refs(
        chunks,
        _merge_extra_filerefs(
            kwargs.get("extra_filerefs", ""), opts.get("extra_filerefs", "")
        ),
    )
    # Check for errors
    for chunk in chunks:
        if not isinstance(chunk, dict):
            __context__["retcode"] = 1
            return chunks

    roster = salt.roster.Roster(opts, opts.get("roster", "flat"))
    roster_grains = roster.opts["grains"]

    # Create the tar containing the state pkg and relevant files.
    _cleanup_slsmod_low_data(chunks)
    trans_tar = salt.client.ssh.state.prep_trans_tar(
        opts,
        __context__["fileclient"],
        chunks,
        file_refs,
        __pillar__,
        st_kwargs["id_"],
        roster_grains,
    )
    trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts["hash_type"])
    cmd = "state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}".format(
        opts["thin_dir"], test, trans_tar_sum, opts["hash_type"]
    )
    single = salt.client.ssh.Single(
        opts,
        cmd,
        fsclient=__context__["fileclient"],
        minion_opts=__salt__.minion_opts,
        **st_kwargs,
    )
    single.shell.send(trans_tar, "{0}/salt_state.tgz".format(opts["thin_dir"]))
    stdout, stderr, _ = single.cmd_block()

    # Clean up our tar
    try:
        os.remove(trans_tar)
    except (OSError, IOError):
        pass

    # Read in the JSON data and return the data structure
    try:
        return salt.utils.json.loads(stdout)
    except Exception as e:
        log.error("JSON Render failed for: %s\n%s", stdout, stderr)
        log.error(six.text_type(e))

    # If for some reason the json load fails, return the stdout
    return stdout