import json
import logging


def save_session(file_name, session):
    """
    Save the session dictionary back to disk in the appropriate folder using
    a temporary name. The name of the file is returned if the new session is
    successfully saved.
    """
    file_name = file_name + ".tmp"
    try:
        with open(file_name, "w", encoding="utf-8") as file:
            json.dump(session, file,
                      indent="\t",
                      ensure_ascii=False,
                      sort_keys=True,
                      separators=(',', ': '))
            return file_name

    except TypeError:
        logging.exception("Session file contains non-basic data")

    except OSError:
        logging.exception("Error saving new session file")

    return None