How to use the tinyrpc.protocols.jsonrpc.JSONRPCInvalidRequestError function in tinyrpc

To help you get started, we’ve selected a few tinyrpc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
def test_jsonrpc_spec_v2_example8(prot):
    try:
        prot.parse_request("""[]""")
        assert False
    except JSONRPCInvalidRequestError as error:
        e = error

    response = e.error_respond()

    assert _json_equal("""{"jsonrpc": "2.0", "error": {"code": -32600,
    "message": "Invalid Request"}, "id": null}""",
           response.serialize())
github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
    (JSONRPCInvalidRequestError, -32600, 'Invalid Request'),
    (JSONRPCMethodNotFoundError, -32601, 'Method not found'),
    (JSONRPCInvalidParamsError, -32602, 'Invalid params'),
    (JSONRPCInternalError, -32603, 'Internal error'),

    # generic errors
    #(InvalidRequestError, -32600, 'Invalid Request'),
    #(MethodNotFoundError, -32601, 'Method not found'),
    #(ServerError, -32603, 'Internal error'),
])
def test_proper_construction_of_error_codes(prot, exc, code, message):
    request = prot.parse_request(
        """{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4],
           "id": "1"}"""
    )
    reply = exc().error_respond().serialize()
    assert isinstance(reply, bytes)
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
def _get_code_message_and_data(error: Union[Exception, str]
                               ) -> Tuple[int, str, Any]:
    assert isinstance(error, (Exception, str))
    data = None
    if isinstance(error, Exception):
        if hasattr(error, 'jsonrpc_error_code'):
            code = error.jsonrpc_error_code
            msg = str(error)
            try:
                data = error.data
            except AttributeError:
                pass
        elif isinstance(error, InvalidRequestError):
            code = JSONRPCInvalidRequestError.jsonrpc_error_code
            msg = JSONRPCInvalidRequestError.message
        elif isinstance(error, MethodNotFoundError):
            code = JSONRPCMethodNotFoundError.jsonrpc_error_code
            msg = JSONRPCMethodNotFoundError.message
        elif isinstance(error, InvalidParamsError):
            code = JSONRPCInvalidParamsError.jsonrpc_error_code
            msg = JSONRPCInvalidParamsError.message
        else:
            # allow exception message to propagate
            code = JSONRPCServerError.jsonrpc_error_code
            if len(error.args) == 2:
                msg = str(error.args[0])
                data = error.args[1]
            else:
                msg = str(error)
    else:
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
try:
            req = json.loads(data)
        except Exception as e:
            raise JSONRPCParseError()

        if isinstance(req, list):
            # batch request
            requests = JSONRPCBatchRequest()
            for subreq in req:
                try:
                    requests.append(self._parse_subrequest(subreq))
                except RPCError as e:
                    requests.append(e)
                except Exception as e:
                    requests.append(JSONRPCInvalidRequestError(request_id=subreq.get("id")))

            if not requests:
                raise JSONRPCInvalidRequestError()
            return requests
        else:
            return self._parse_subrequest(req)
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
def _parse_subrequest(self, req):
        if not isinstance(req, dict):
            raise JSONRPCInvalidRequestError()

        for k in req.keys():
            if k not in self._ALLOWED_REQUEST_KEYS:
                raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if not isinstance(req['method'], str):
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        request = self.request_factory()

        request.method = req['method']
        request.one_way = 'id' not in req
        if not request.one_way:
            request.unique_id = req['id']

        params = req.get('params', None)
        if params is not None:
            if isinstance(params, list):
                request.args = req['params']
            elif isinstance(params, dict):
                request.kwargs = req['params']
            else:
                raise JSONRPCInvalidParamsError(request_id=req.get("id"))
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
def _parse_subrequest(self, req):
        if not isinstance(req, dict):
            raise JSONRPCInvalidRequestError()

        for k in req.keys():
            if k not in self._ALLOWED_REQUEST_KEYS:
                raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if not isinstance(req['method'], str):
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        request = self.request_factory()

        request.method = req['method']
        request.one_way = 'id' not in req
        if not request.one_way:
            request.unique_id = req['id']

        params = req.get('params', None)
        if params is not None:
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
def _get_code_message_and_data(error: Union[Exception, str]
                               ) -> Tuple[int, str, Any]:
    assert isinstance(error, (Exception, str))
    data = None
    if isinstance(error, Exception):
        if hasattr(error, 'jsonrpc_error_code'):
            code = error.jsonrpc_error_code
            msg = str(error)
            try:
                data = error.data
            except AttributeError:
                pass
        elif isinstance(error, InvalidRequestError):
            code = JSONRPCInvalidRequestError.jsonrpc_error_code
            msg = JSONRPCInvalidRequestError.message
        elif isinstance(error, MethodNotFoundError):
            code = JSONRPCMethodNotFoundError.jsonrpc_error_code
            msg = JSONRPCMethodNotFoundError.message
        elif isinstance(error, InvalidParamsError):
            code = JSONRPCInvalidParamsError.jsonrpc_error_code
            msg = JSONRPCInvalidParamsError.message
        else:
            # allow exception message to propagate
            code = JSONRPCServerError.jsonrpc_error_code
            if len(error.args) == 2:
                msg = str(error.args[0])
                data = error.args[1]
            else:
                msg = str(error)
    else:
        code = -32000
github mbr / tinyrpc / tinyrpc / protocols / jsonrpc.py View on Github external
def _parse_subrequest(self, req):
        if not isinstance(req, dict):
            raise JSONRPCInvalidRequestError()

        for k in req.keys():
            if k not in self._ALLOWED_REQUEST_KEYS:
                raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        if not isinstance(req['method'], str):
            raise JSONRPCInvalidRequestError(request_id=req.get("id"))

        request = self.request_factory()

        request.method = req['method']
        request.one_way = 'id' not in req
        if not request.one_way: