import json


def format_rpc_response(data, exception=None):
    """
    Formats a response from a RPC Manager.
    It provides the data and/or a serialized exception so it can be
    re-created by the caller.

    :param Any data: A JSON Serializable object.
    :param Exception exception: An Exception object
    :return str: JSON Response.
    """
    exception_data = None
    if exception:
        args = exception.__getargs__() if hasattr(exception, "__getargs__") else exception.args
        kwargs = exception.__getkwargs__() if hasattr(exception, "__getkwargs__") else {}
        if kwargs is None: kwargs = {}
        try:
            module = exception.__module__
        except:
            module = None

        exception_data = {
            'exception': type(exception).__name__,
            'message': str(exception),
            'args': args,
            "kwargs": kwargs,
            'module': module,
        }

    return json.dumps({
        'data': data,
        'exception': exception_data
    })