Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
# 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 | ||||
from collections import abc | from collections import abc | ||||
import functools | import functools | ||||
import inspect | import inspect | ||||
import logging | import logging | ||||
▲ Show 20 Lines • Show All 305 Lines • ▼ Show 20 Lines | class RPCClient(metaclass=MetaRPCClient): | ||||
def raise_for_status(self, response) -> None: | def raise_for_status(self, response) -> None: | ||||
"""check response HTTP status code and raise an exception if it denotes an | """check response HTTP status code and raise an exception if it denotes an | ||||
error; do nothing otherwise | error; do nothing otherwise | ||||
""" | """ | ||||
status_code = response.status_code | status_code = response.status_code | ||||
status_class = response.status_code // 100 | status_class = response.status_code // 100 | ||||
if status_code == 404: | |||||
raise RemoteException(payload="404 not found", response=response) | |||||
exception = None | exception = None | ||||
# TODO: only old servers send pickled error; stop trying to unpickle | # TODO: only old servers send pickled error; stop trying to unpickle | ||||
# after they are all upgraded | # after they are all upgraded | ||||
try: | try: | ||||
if status_class == 4: | if status_class == 4: | ||||
data = self._decode_response(response, check_status=False) | data = self._decode_response(response, check_status=False) | ||||
if isinstance(data, dict): | if isinstance(data, dict): | ||||
Show All 17 Lines | def raise_for_status(self, response) -> None: | ||||
# TODO: remove "exception" key check once all servers | # TODO: remove "exception" key check once all servers | ||||
# are using new schema | # are using new schema | ||||
exc_data = data["exception"] if "exception" in data else data | exc_data = data["exception"] if "exception" in data else data | ||||
exception = RemoteException(payload=exc_data, response=response) | exception = RemoteException(payload=exc_data, response=response) | ||||
except (TypeError, pickle.UnpicklingError): | except (TypeError, pickle.UnpicklingError): | ||||
raise RemoteException(payload=data, response=response) | raise RemoteException(payload=data, response=response) | ||||
if status_code == 404 and type(exception) not in self.reraise_exceptions: | |||||
raise RemoteException(payload="404 not found", response=response) | |||||
vlorentz: I think that's equivalent here, given how `exception` is instantiated | |||||
anlambertAuthorUnsubmitted Done Inline ActionsNo we still need to check if the exception should be reraised or a RemoteException will be raised otherwise. anlambert: No we still need to check if the exception should be reraised or a `RemoteException` will be… | |||||
if exception: | if exception: | ||||
raise exception from None | raise exception from None | ||||
if status_class != 2: | if status_class != 2: | ||||
raise RemoteException( | raise RemoteException( | ||||
payload=f"API HTTP error: {status_code} {response.content}", | payload=f"API HTTP error: {status_code} {response.content}", | ||||
response=response, | response=response, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 153 Lines • Show Last 20 Lines |
I think that's equivalent here, given how exception is instantiated