Beispiel #1
0
def process():

    responseBody = {
        'version': '1.1',
        'error': None,
        'result': None
    }
    requestBody = None
    try:
        # init/clear fossil cache
        clearCache()

        # read request
        try:
            requestBody = request.get_json()
            Logger.get('rpc').info('json rpc request. request: {0}'.format(requestBody))
        except BadRequest:
            raise RequestError('ERR-R1', 'Invalid mime-type.')
        if not requestBody:
            raise RequestError('ERR-R2', 'Empty request.')
        if 'id' in requestBody:
            responseBody['id'] = requestBody['id']

        # run request
        responseBody['result'] = ServiceRunner().invokeMethod(str(requestBody['method']),
                                                              requestBody.get('params', []))
    except CausedError as e:
        try:
            errorInfo = fossilize(e)
        except NonFossilizableException as e2:
            # catch Exceptions that are not registered as Fossils
            # and log them
            errorInfo  = {'code': '', 'message': str(e2)}
            Logger.get('dev').exception('Exception not registered as fossil')

        # NoReport errors (i.e. not logged in) shouldn't be logged
        if not isinstance(e, NoReportError):
            Logger.get('rpc').exception('Service request failed. '
                                        'Request text:\r\n{0}\r\n\r\n'.format(requestBody))

            if requestBody:
                params = requestBody.get('params', [])
                Sanitization._escapeHTML(params)
                errorInfo["requestInfo"] = {
                    'method': str(requestBody['method']),
                    'params': params,
                    'origin': str(requestBody.get('origin', 'unknown'))
                }
                Logger.get('rpc').debug('Arguments: {0}'.format(errorInfo['requestInfo']))
        responseBody['error'] = errorInfo

    try:
        jsonResponse = dumps(responseBody, ensure_ascii=True)
    except UnicodeError:
        Logger.get('rpc').exception('Problem encoding JSON response')
        # This is to avoid exceptions due to old data encodings (based on iso-8859-1)
        responseBody['result'] = fix_broken_obj(responseBody['result'])
        jsonResponse = encode(responseBody)

    return app.response_class(jsonResponse, mimetype='application/json')
Beispiel #2
0
def process():

    responseBody = {"version": "1.1", "error": None, "result": None}
    requestBody = None
    try:
        # init/clear fossil cache
        clearCache()

        # read request
        try:
            requestBody = request.get_json()
            Logger.get("rpc").info("json rpc request. request: {0}".format(requestBody))
        except BadRequest:
            raise RequestError("ERR-R1", "Invalid mime-type.")
        if not requestBody:
            raise RequestError("ERR-R2", "Empty request.")
        if "id" in requestBody:
            responseBody["id"] = requestBody["id"]

        # run request
        responseBody["result"] = ServiceRunner().invokeMethod(str(requestBody["method"]), requestBody.get("params", []))
    except CausedError as e:
        try:
            errorInfo = fossilize(e)
        except NonFossilizableException as e2:
            # catch Exceptions that are not registered as Fossils
            # and log them
            errorInfo = {"code": "", "message": str(e2)}
            Logger.get("dev").exception("Exception not registered as fossil")

        # NoReport errors (i.e. not logged in) shouldn't be logged
        if not isinstance(e, NoReportError):
            Logger.get("rpc").exception("Service request failed. " "Request text:\r\n{0}\r\n\r\n".format(requestBody))

            if requestBody:
                params = requestBody.get("params", [])
                Sanitization._escapeHTML(params)
                errorInfo["requestInfo"] = {
                    "method": str(requestBody["method"]),
                    "params": params,
                    "origin": str(requestBody.get("origin", "unknown")),
                }
                Logger.get("rpc").debug("Arguments: {0}".format(errorInfo["requestInfo"]))
        responseBody["error"] = errorInfo

    try:
        jsonResponse = dumps(responseBody, ensure_ascii=True)
    except UnicodeError:
        Logger.get("rpc").exception("Problem encoding JSON response")
        # This is to avoid exceptions due to old data encodings (based on iso-8859-1)
        responseBody["result"] = fix_broken_obj(responseBody["result"])
        jsonResponse = encode(responseBody)

    return app.response_class(jsonResponse, mimetype="application/json")