import typing
import json


def attempt_json_write(
        path: str,
        contents: dict,
        mode: str = 'w'
) -> typing.Union[None, Exception]:
    """
    Attempts to write the specified JSON content to file.

    :param path:
        The path to the file where the JSON serialized content will be written.
    :param contents:
        The JSON data to write to the file
    :param mode:
        The mode used to open the file where the content will be written.
    :return:
        None if the write operation succeeded. Otherwise, the exception that
        was raised by the failed write operation.
    """
    try:
        with open(path, mode) as f:
            json.dump(contents, f)
        return None
    except Exception as error:
        return error