# Buggy code
def returner(load):
    """
    Return data to a postgres server
    """
    conn = _get_conn()
    if conn is None:
        return None
    cur = conn.cursor()
    sql = """INSERT INTO salt_returns
            (fun, jid, return, id, success)
            VALUES (%s, %s, %s, %s, %s)"""
    job_ret = {
        "return": six.text_type(six.text_type(load["return"]), "utf-8", "replace")
    }
    if "retcode" in load:
        job_ret["retcode"] = load["retcode"]
    if "success" in load:
        job_ret["success"] = load["success"]
    cur.execute(
        sql,
        (
            load["fun"],
            load["jid"],
            salt.utils.json.dumps(job_ret),
            load["id"],
            load.get("success"),
        ),
    )
    _close_conn(conn)


# Traceback (TypeError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: decoding Unicode is not supported
Traceback (most recent call last):
File "/usr/bin/salt-run", line 10, in <module>
salt_run()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 412, in salt_run
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/run.py", line 40, in run
ret = runner.run()
File "/usr/lib/python2.7/dist-packages/salt/runner.py", line 271, in run
daemonize=False)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 485, in _proc_function
return self.low(fun, low, full_return=False)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 261, in low
return self._low(fun, low, print_event=print_event, full_return=full_return)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 418, in _low
mminion=self.mminion,
File "/usr/lib/python2.7/dist-packages/salt/utils/job.py", line 110, in store_job
mminion.returners[fstr](load)
File "/usr/lib/python2.7/dist-packages/salt/returners/postgres_local_cache.py", line 235, in returner
job_ret = {'return': six.text_type(six.text_type(load['return']), 'utf-8', 'replace')}
TypeError: decoding Unicode is not supported
Traceback (most recent call last):
File "/usr/bin/salt-run", line 10, in <module>
salt_run()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 412, in salt_run
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/run.py", line 40, in run
ret = runner.run()
File "/usr/lib/python2.7/dist-packages/salt/runner.py", line 271, in run
daemonize=False)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 485, in _proc_function
return self.low(fun, low, full_return=False)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 261, in low
return self._low(fun, low, print_event=print_event, full_return=full_return)
File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 418, in _low
mminion=self.mminion,
File "/usr/lib/python2.7/dist-packages/salt/utils/job.py", line 110, in store_job
mminion.returners[fstr](load)
File "/usr/lib/python2.7/dist-packages/salt/returners/postgres_local_cache.py", line 235, in returner
job_ret = {'return': six.text_type(six.text_type(load['return']), 'utf-8', 'replace')}
TypeError: decoding Unicode is not supported

# Fixed code
def returner(load):
    """
    Return data to a postgres server
    """
    conn = _get_conn()
    if conn is None:
        return None
    cur = conn.cursor()
    sql = """INSERT INTO salt_returns
            (fun, jid, return, id, success)
            VALUES (%s, %s, %s, %s, %s)"""
    try:
        ret = six.text_type(load["return"])
    except UnicodeDecodeError:
        ret = str(load["return"])
    job_ret = {"return": ret}
    if "retcode" in load:
        job_ret["retcode"] = load["retcode"]
    if "success" in load:
        job_ret["success"] = load["success"]
    cur.execute(
        sql,
        (
            load["fun"],
            load["jid"],
            salt.utils.json.dumps(job_ret),
            load["id"],
            load.get("success"),
        ),
    )
    _close_conn(conn)