Skip to content
Snippets Groups Projects
Commit e6497914 authored by vlorentz's avatar vlorentz
Browse files

Remove support for deprecated exception format

It was removed from all RPC servers years ago.
parent 2d1cd6af
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2015-2020 The Software Heritage developers # Copyright (C) 2015-2022 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution # See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version # License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
...@@ -7,7 +7,6 @@ from collections import abc ...@@ -7,7 +7,6 @@ from collections import abc
import functools import functools
import inspect import inspect
import logging import logging
import pickle
from typing import ( from typing import (
Any, Any,
Callable, Callable,
...@@ -325,36 +324,18 @@ class RPCClient(metaclass=MetaRPCClient): ...@@ -325,36 +324,18 @@ class RPCClient(metaclass=MetaRPCClient):
exception = None exception = None
# TODO: only old servers send pickled error; stop trying to unpickle if status_class == 4:
# after they are all upgraded exc_data = self._decode_response(response, check_status=False)
try: for exc_type in self.reraise_exceptions:
if status_class == 4: if exc_type.__name__ == exc_data["type"]:
data = self._decode_response(response, check_status=False) exception = exc_type(*exc_data["args"])
if isinstance(data, dict): break
# TODO: remove "exception" key check once all servers else:
# are using new schema exception = RemoteException(payload=exc_data, response=response)
exc_data = data["exception"] if "exception" in data else data
for exc_type in self.reraise_exceptions: elif status_class == 5:
if exc_type.__name__ == exc_data["type"]: exc_data = self._decode_response(response, check_status=False)
exception = exc_type(*exc_data["args"]) exception = RemoteException(payload=exc_data, response=response)
break
else:
exception = RemoteException(payload=exc_data, response=response)
else:
exception = pickle.loads(data)
elif status_class == 5:
data = self._decode_response(response, check_status=False)
if "exception_pickled" in data:
exception = pickle.loads(data["exception_pickled"])
else:
# TODO: remove "exception" key check once all servers
# are using new schema
exc_data = data["exception"] if "exception" in data else data
exception = RemoteException(payload=exc_data, response=response)
except (TypeError, pickle.UnpicklingError):
raise RemoteException(payload=data, response=response)
if exception: if exception:
raise exception from None raise exception from None
......
# Copyright (C) 2018-2019 The Software Heritage developers # Copyright (C) 2018-2022 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution # See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version # License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
...@@ -110,13 +110,11 @@ def test_client_connexion_error(rpc_client, requests_mock): ...@@ -110,13 +110,11 @@ def test_client_connexion_error(rpc_client, requests_mock):
assert str(exc_info.value.args[0]) == error_message assert str(exc_info.value.args[0]) == error_message
def _exception_response(exception, status_code, old_exception_schema=False): def _exception_response(exception, status_code):
def callback(request, context): def callback(request, context):
assert request.headers["Content-Type"] == "application/x-msgpack" assert request.headers["Content-Type"] == "application/x-msgpack"
context.headers["Content-Type"] = "application/x-msgpack" context.headers["Content-Type"] = "application/x-msgpack"
exc_dict = exception_to_dict(exception) exc_dict = exception_to_dict(exception)
if old_exception_schema:
exc_dict = {"exception": exc_dict}
context.content = msgpack_dumps(exc_dict) context.content = msgpack_dumps(exc_dict)
context.status_code = status_code context.status_code = status_code
return context.content return context.content
...@@ -124,8 +122,7 @@ def _exception_response(exception, status_code, old_exception_schema=False): ...@@ -124,8 +122,7 @@ def _exception_response(exception, status_code, old_exception_schema=False):
return callback return callback
@pytest.mark.parametrize("old_exception_schema", [False, True]) def test_client_reraise_exception(rpc_client, requests_mock):
def test_client_reraise_exception(rpc_client, requests_mock, old_exception_schema):
""" """
Exception caught server-side and whitelisted will be raised again client-side. Exception caught server-side and whitelisted will be raised again client-side.
""" """
...@@ -137,7 +134,6 @@ def test_client_reraise_exception(rpc_client, requests_mock, old_exception_schem ...@@ -137,7 +134,6 @@ def test_client_reraise_exception(rpc_client, requests_mock, old_exception_schem
content=_exception_response( content=_exception_response(
exception=ReraiseException(error_message), exception=ReraiseException(error_message),
status_code=400, status_code=400,
old_exception_schema=old_exception_schema,
), ),
) )
...@@ -147,18 +143,8 @@ def test_client_reraise_exception(rpc_client, requests_mock, old_exception_schem ...@@ -147,18 +143,8 @@ def test_client_reraise_exception(rpc_client, requests_mock, old_exception_schem
assert str(exc_info.value) == error_message assert str(exc_info.value) == error_message
@pytest.mark.parametrize( @pytest.mark.parametrize("status_code", [400, 500])
"status_code, old_exception_schema", def test_client_raise_remote_exception(rpc_client, requests_mock, status_code):
[
(400, False),
(500, False),
(400, True),
(500, True),
],
)
def test_client_raise_remote_exception(
rpc_client, requests_mock, status_code, old_exception_schema
):
""" """
Exception caught server-side and not whitelisted will be wrapped and raised Exception caught server-side and not whitelisted will be wrapped and raised
as a RemoteException client-side. as a RemoteException client-side.
...@@ -171,7 +157,6 @@ def test_client_raise_remote_exception( ...@@ -171,7 +157,6 @@ def test_client_raise_remote_exception(
content=_exception_response( content=_exception_response(
exception=Exception(error_message), exception=Exception(error_message),
status_code=status_code, status_code=status_code,
old_exception_schema=old_exception_schema,
), ),
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment